C#添加WebService服务实现天气预报

用C#做一个类似于上图的天气查询:

首先引用webservice服务
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="WeatherService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <applicationSettings>
        <WeatherService.Properties.Settings>
            <setting name="WeatherService_Weather_WeatherWebService" serializeAs="String">
                <value>http://www.webxml.com.cn/WebServices/WeatherWebService.asmx</value>
            </setting>
        </WeatherService.Properties.Settings>
    </applicationSettings>
</configuration>

然后创建接口:

using System.ServiceModel;


namespace WeatherService
{
   
    interface Interface
    {
   
        [OperationContract]

//得到天气具体情况
        string[] GetWeatherbyCityName(string cityname);
        [OperationContract]

//根据省份获取对应的城市
        string[] GetSupportCity(string proviceName);
        [OperationContract]

//获取省份
        string[] GetSupportProvince();
    }
}

实现接口

namespace WeatherService
{
   
    class WeatherServer : Interface
    {
   
        public string[] GetSupportCity(string proviceName)
        {
   

//获取服务对象  调用网站提供的方法
            Weather.WeatherWebService service = new Weather.WeatherWebService();
            return service.getSupportCity(proviceName);
        }


        public string[] GetSupportProvince()
        {
   
            Weather.WeatherWebService service = new Weather.WeatherWebService();
            return service.getSupportProvince();


        }

   public string[]  GetWeatherbyCityName(string cityname)
        {
   
            Weather.WeatherWebService service = new Weather.WeatherWebService();
            string[] s = new string[23];
            s = service.getWeatherbyCityName(cityname);
            return s;
        }

}
创建界面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.Security.Policy;


namespace WeatherService
{
   
   /// <summary>
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值