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("")]
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
界面如下

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);
}
}
}
}
25.C# 异步调用Web服务的更多相关文章
- mvc路由引起异步调用web服务的问题
从一篇blog得知使用脚本可以异步调用Web服务,觉得很新鲜,因为自己很少用到Web服务,所以决定写一写看看什么效果. 首先在UI项目(我使用的是MVC4.0)里创建一个Web服务. 添加Web服务后 ...
- HttpClient异步调用WEB服务
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...
- 动态调用web服务
通常我们在程序中需要调用WebService时,都是通过“添加Web引用”,让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务.这样是使工作简单了,但是却和提供Web服务的URL.方法名 ...
- Android调用Web服务
现在大部分应用程序都把业务逻辑处理,数据调用等功能封装成了服务的形式,应用程序只需要调用这些web服务就好了,在这里就不赘述web服务的优点了.本文总结如何在android中调用Web服务,通过传递基 ...
- 使用ASP.NET AJAX 从脚本中调用Web 服务的应用方法
技能点:通过编写WebService,在页面js中调用WebService来进行数据查询. 网站开发,有些时候需要使用js在页面动态生成一些内容,但还有些数据要通过查询数据库才能获取的. 但由于诸如主 ...
- 在 SQL Server 的存储过程中调用 Web 服务
介绍 一个老朋友计划开发一个应用,基于 .NET 和 Socket,但需要在存储过程中调用 Web 服务. 在这篇文章中我们将分享这个应用的经验,讲述如何在存储过程中调用 Web 服务,并传递参数. ...
- Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token
原文:Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务.WCF消息头添加安全验证Token 为什么选择wcf? 因为好像wcf和wpf就是哥俩,,, 为什么选择异步 ...
- 动态调用web服务 --WSHelper.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Net;us ...
- 序列化和反序列化,异步调用web/wcf/函数
//xml序列化 public static string Seria(DataSet ds) { XmlSerializer serializer = new XmlSerializer(typeo ...
随机推荐
- mac的公式编辑器: mathtype/latex
mathtype 收费版,且马上不能在mac系统上使用 latex 搜索一下: ➜ ~ brew search latex ==> Formulae gnome-latex latex2html ...
- Django:序列化的几种方法
前言 关于序列化操作,就是将一个可迭代的数据结构,通过便利的方式进行我们所需要的操作. 今天历来归纳一下,Django中的几种不同得分方法,已经Django-restframework提供的方法 创建 ...
- 微信公众号使用vue,安卓端点击按钮404,ios访问正常问题
情景:微信公众号使用vue开发的单页面,在安卓端点击按钮访问显示404,ios访问正常问题,能正常显示. 解决:将微信公众号菜单按钮设置的路径中把WWW去掉后,安卓.ios都能正常访问. 问题路径ww ...
- Mowing the Lawn【线性dp + 单调队列优化】
题目链接:https://ac.nowcoder.com/acm/contest/2652/G 题目大意:与上一篇博客 烽火传递 差不多. 1.一共n头羊,若超过m头连续的羊在一起,就会集体罢工,每头 ...
- MacOS上使用Openconnect代替Cisco Anyconnect
OpenConnect是一个Cisco Anyconnect的替代品,具有开源.易获取.可靠等优点.而官方版本的Cisco Anyconnect配置较为繁琐,需要在管理界面同时部署多平台客户端才能支持 ...
- kill 掉 占据端口的进程
netstat -anp | grep 80kill -9 进程号
- xsy 2018 【NOIP2013】货车运输
[NOIP2013]货车运输 Description A 国有n座城市,编号从1到n,城市之间有m条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有q辆货车在运输货物,司机们想知道每辆车在不超 ...
- QT 创建一个线程播放监控视频
1.创建一个线程类(PlayVideoThread): PlayVideoThread.h头文件 #pragma once #include <QObject> #include &quo ...
- 链表习题(1)-设计一个递归算法,删除不带头结点的单链表L中所有值为x的结点
/*设计一个递归算法,删除不带头结点的单链表L中所有值为x的结点*/ /* 算法思想:设f(L,x)的功能是删除以L为首结点指针的单链表中所有值等于x的结点, 则显然有f(L->next,x)的 ...
- go select 的default
当 select 中的其他条件分支都没有准备好的时候,`default` 分支会被执行. 为了非阻塞的发送或者接收,可使用 default 分支: select { case i := <-c: ...