25.C# 异步调用Web服务

时间:2022-12-01 19:58:20

1.创建Web服务

1.1VS新建ASP.Net空Web应用程序

25.C# 异步调用Web服务

1.2添加Web服务新建项

25.C# 异步调用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("")]
public class WebService1 : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
} [WebMethod]
public string ReverseString(string message)
{
if (message.Contains(""))
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之间的一个数
if (req.TemperatureType == TemperatureType.Celsius)
resp.Temperature = celsius;
else
resp.Temperature = ( - ) / * celsius + ;//摄氏度转换成华氏温度 if (req.City == "RedMond")
resp.TemperatureCondition = TemperatureCondition.Rainy;
else
resp.TemperatureCondition = (TemperatureCondition)r.Next(, );//随机取出一个天气 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服务

2.1新建Winfrom应用程序WebServiceSample

界面如下

25.C# 异步调用Web服务

2.2添加服务引用

在引用右键添加服务引用,输入webservice地址

25.C# 异步调用Web服务

高级里面勾选生成异步操作

25.C# 异步调用Web服务

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);
}
}
}
}