Webサービスへの25.C#非同期呼び出し

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; }//温度
    }
}
View Code

2.调用web服务

2.1新建Winfrom应用程序WebServiceSample

界面如下

2.2添加服务引用

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

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

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

 

おすすめ

転載: www.cnblogs.com/lidaying5/p/10939174.html