• 25.C# 异步调用Web服务


    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
  • 相关阅读:
    git 学习笔记 ---远程仓库
    git学习笔记 ---删除文件
    git 学习笔记 ---撤销修改
    git学习笔记 ---管理修改
    git学习笔记 ---工作区和暂存区
    git学习笔记 ---版本退回
    git 学习笔记 ---安装
    windows删除或修改本地Git保存的账号密码
    win10企业版永久激活方法
    IntelliJ IDEA 插件开发视频教程
  • 原文地址:https://www.cnblogs.com/lidaying5/p/10939174.html
Copyright © 2020-2023  润新知