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服务的更多相关文章

  1. mvc路由引起异步调用web服务的问题

    从一篇blog得知使用脚本可以异步调用Web服务,觉得很新鲜,因为自己很少用到Web服务,所以决定写一写看看什么效果. 首先在UI项目(我使用的是MVC4.0)里创建一个Web服务. 添加Web服务后 ...

  2. HttpClient异步调用WEB服务

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...

  3. 动态调用web服务

    通常我们在程序中需要调用WebService时,都是通过“添加Web引用”,让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务.这样是使工作简单了,但是却和提供Web服务的URL.方法名 ...

  4. Android调用Web服务

    现在大部分应用程序都把业务逻辑处理,数据调用等功能封装成了服务的形式,应用程序只需要调用这些web服务就好了,在这里就不赘述web服务的优点了.本文总结如何在android中调用Web服务,通过传递基 ...

  5. 使用ASP.NET AJAX 从脚本中调用Web 服务的应用方法

    技能点:通过编写WebService,在页面js中调用WebService来进行数据查询. 网站开发,有些时候需要使用js在页面动态生成一些内容,但还有些数据要通过查询数据库才能获取的. 但由于诸如主 ...

  6. 在 SQL Server 的存储过程中调用 Web 服务

    介绍 一个老朋友计划开发一个应用,基于 .NET 和 Socket,但需要在存储过程中调用 Web 服务. 在这篇文章中我们将分享这个应用的经验,讲述如何在存储过程中调用 Web 服务,并传递参数. ...

  7. Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token

    原文:Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务.WCF消息头添加安全验证Token 为什么选择wcf?   因为好像wcf和wpf就是哥俩,,, 为什么选择异步 ...

  8. 动态调用web服务 --WSHelper.cs

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Net;us ...

  9. 序列化和反序列化,异步调用web/wcf/函数

    //xml序列化 public static string Seria(DataSet ds) { XmlSerializer serializer = new XmlSerializer(typeo ...

随机推荐

  1. windows下 pip下载包到指定目录

    pip download -r requirements.txt -d G:\PythonVirtualenv\packages python setup.py install

  2. 【VS开发】windows下的signal

    在windows下,信号机制简单来说是通过工作线程实现的,该线程运行于相对优先级THREAD_PRIORITY_HIGHEST,当信号产生时,windows生成该线程执行信号处理逻辑,由于该线程优先级 ...

  3. 最新 博盾习言java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿. 博盾习言等10家互联网公司的校招Offer,因为某些自身原因最终选择了 博盾习言.6.7月主要是做系统复习.项目复盘.Le ...

  4. Ansible安装配置及命令使用详解

    Ansible和saltstack目前市面上一些其它的项目管理工具有很大的不同,它的设计初衷就是为了更方便.快捷的进行配置管理.它易于安装和使用.语法也非常简单易学.你可以用Ansible将平常复杂的 ...

  5. Ubuntu下U盘文件只读的解决办法

    转自linux公社:http://www.linuxidc.com/Linux/2012-12/76130.htm Ubuntu下U盘出现文件系统只读的问题了.症状为无法删除U盘中现有文件,无法向U盘 ...

  6. 自己实现CountDownLatch

    自己实现的CountDownLatch ,只是模拟他的功能而已.jdk中的实现采用的是AQS public class MyCountDownLatch { private final int tot ...

  7. 使用RestTemplate进行服务调用的几种方式

    首先我们在名为MSG的服务中定义一个简单的方法 @RestController public class ServerController { @GetMapping("/msg" ...

  8. 给定a、b两个文件,各存放50亿个url,每个url各占64字节,内存限制是4G,让你找出a、b文件共同的url?

    package com.hadoop.hdfs; import org.apache.hadoop.yarn.webapp.hamlet.Hamlet; import org.junit.Test; ...

  9. python 之 面向对象基础(继承与派生,经典类与新式类)

    7.2 继承与派生 7.21继承 1.什么是继承? 继承是一种新建类的的方式,在python中支持一个子类继承多个父类.新建的类称为子类或者派生类,父类又可以称为基类或者超类,子类会”遗传“父类的属性 ...

  10. docker 实践四:数据管理

    这篇是关于 docker 的数据管理. 注:环境为 CentOS7,docker 19.03. 一般容器中管理数据主要有两种方式: 数据卷(Data Volumes):容器内数据直接映射到本地主机环境 ...