1.创建Web服务
1.1VS新建ASP.Net空Web应用程序
1.2添加Web服务新建项
1.3添加GetWeather方法和相关类
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.EnterpriseServices; namespace WebService { /// <summary> /// WebService1 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/",Name ="WebServiceTest" ,Description ="test" ) ] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] [Description("222")] public class WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public string ReverseString(string message) { if (message.Contains("1")) throw new Exception("不能包含1"); else return new string( message.Reverse().ToArray()); } [WebMethod] public GetWeatherResponse GetWeather(GetWeatherRequest req) { GetWeatherResponse resp = new GetWeatherResponse(); Random r =new Random(); int celsius = r.Next(-20, 50);//返回-20到50之间的一个数 if (req.TemperatureType == TemperatureType.Celsius) resp.Temperature = celsius; else resp.Temperature = (212 - 32) / 100 * celsius + 32;//摄氏度转换成华氏温度 if (req.City == "RedMond") resp.TemperatureCondition = TemperatureCondition.Rainy; else resp.TemperatureCondition = (TemperatureCondition)r.Next(0, 3);//随机取出一个天气 return resp; } } public enum TemperatureType { Fahrenheit,//华氏温度 Celsius//摄氏度 } public class GetWeatherRequest { public string City { get; set; } public TemperatureType TemperatureType { get; set; } } /// <summary> /// 天气情况 /// </summary> public enum TemperatureCondition { Rainy, Sunny, Cloudy, Thunderstorms//雷暴天气 } public class GetWeatherResponse { public TemperatureCondition TemperatureCondition { get; set; } public int Temperature { get; set; }//温度 } }