1.建立Web服務web
1.1VS新建ASP.Net空Web應用程序dom
1.2添加Web服務新建項異步
1.3添加GetWeather方法和相關類ide
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; }//溫度 } }
2.調用web服務spa
2.1新建Winfrom應用程序WebServiceSamplecode
界面以下orm
2.2添加服務引用blog
在引用右鍵添加服務引用,輸入webservice地址ip
高級裏面勾選生成異步操做get
2.3 在GetWeather按鈕中 調用web服務,代碼以下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using WebServiceSample.WebServiceTest; namespace WebServiceSample { public partial class GetWeatherForm : Form { public GetWeatherForm() { InitializeComponent(); } private void btnGetWeather_Click(object sender, EventArgs e) { GetWeatherRequest req = new GetWeatherRequest(); if(radioCelsius.Checked) req.TemperatureType = TemperatureType.Celsius; else req.TemperatureType =TemperatureType.Fahrenheit ; req.City = txtCity.Text; WebServiceTestSoapClient client = new WebServiceTestSoapClient(); client.GetWeatherCompleted += GetWeatherCompleted; client.GetWeatherAsync(req); } void GetWeatherCompleted(object source, GetWeatherCompletedEventArgs e) { if (e.Error == null) { txtTemperature.Text = e.Result.Temperature.ToString(); txtWeatherCondition.Text = e.Result.TemperatureCondition.ToString(); } else { MessageBox.Show(e.Error.Message); } } } }