转自:http://blog.csdn.net/Wgf2006/article/details/2270332
1、使用事件驱动的异步模式的异步 Web 服务客户端
Web 服务代理类中的每个 Web 方法都具有一个异步的对应版本。代理类自动生成每个 Web 方法的异步方法和对应事件。当异步方法被调用时,它将在另一个线程上执行,并在返回时引发对应的事件。您可以为异步方法的对应事件创建一个处理程序,从而 在它返回时执行代码。
此模型可以简化生成多线程应用程序的过程,因为您不必自己实现复杂的多线程代码。
具体做法:
1、 创建MethodCompleted 事件。
private void HelloWorldCompleted(Object sender,
localhost.HelloWorldCompletedEventArgs Completed)
{
// Insert code to implement the method here
}
2、 向该事件的处理程序列表添加 MethodCompleted 事件处理程序。
myWebService.HelloWorldCompleted += new localhost.HelloWorldCompletedEventHandler(HelloWorldCompleted);
3、 使用该方法的 MethodAsync 形式调用 Web 方法.
HelloWorldAsync();
2、使用 Begin/End 模式的回调方法
具体做法:
- 定义实现 AsyncCallback 委托的回调函数。
public static void FactorizeCallback(IAsyncResult ar)
- 实例化 AsyncCallback 委托。
AsyncCallback cb = new AsyncCallback(TestCallback.FactorizeCallback);
- 调用 Begin 方法,将回调函数作为第二个参数传递,将提供状态的对象(在本示例中是 PrimeFactorizer的客户端实现)作为第三个参数传递。
IAsyncResult ar = pf.BeginFactorize(factorizableNum, cb, pf);
- 在 Begin 方法返回的 IAsyncResult 上检查 IsCompleted 属性。如果客户端已收到服务器的响应,该值将设置为 true。
- 在回调函数中访问状态对象。IAsyncState 参数的 AsyncState 属性将该对象作为第三个参数传递给 Begin方法。
PrimeFactorizer pf = (PrimeFactorizer) ar.AsyncState;
- 在回调函数中,对上一步中获取的状态对象调用 End 方法。
long[] results = pf.EndFactorize(ar);
如果没有Begin/end 的web方法 :
1. 做为旧的Web Service使用(Web Reference)
生成的WeatherWebService没有Begin/End方法
关闭project后用notepad编辑对应csproj文件。更改完成后重新打开project,选中Web References下面对应的Web Refrence右键菜单中选择“更新Web引用”,更新完后就有Begin/End方法了。
1 <?xml version="1.0" encoding="utf-8"?> 2 <Project ToolsVersion="4.0" DefaultTargets="Build" 3 4 xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 5 <PropertyGroup> 6 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> 7 <Platform Condition=" '$(Platform)' == '' ">x86</Platform> 8 <ProductVersion>8.0.30703</ProductVersion> 9 <SchemaVersion>2.0</SchemaVersion> 10 <ProjectGuid>{DECB98A2-0FC4-46B5-9857-D828F4332E8F}</ProjectGuid> 11 <OutputType>WinExe</OutputType> 12 <WebReference_EnableLegacyEventingModel>true</WebReference_EnableLegacyEventingModel> 13 <AppDesignerFolder>Properties</AppDesignerFolder> 14 <RootNamespace>Test</RootNamespace> 15 <AssemblyName>Test</AssemblyName> 16 <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> 17 <TargetFrameworkProfile>Client</TargetFrameworkProfile> 18 <FileAlignment>512</FileAlignment> 19 </PropertyGroup> 20 <!-- 省略掉其他内容 --> 21 </Project>
实例
1、
1 /// <summary> 2 /// 事件驱动的异步模式的异步 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 protected void EventDriveButton_Click(object sender, EventArgs e) 7 { 8 Response.Write("事件驱动的异步模式的异步 <BR>"); 9 LocalService.WebService localService = new LocalService.WebService(); //创建代理类对象。 10 localService.HelloWorldCompleted += new LocalService.HelloWorldCompletedEventHandler((o, a) => { Response.Write(string.Format("完成时间:{0}。 结果{1}<Br>", DateTime.Now.ToString("mm:ss.ffff"), a.Result)); });//创建事件。写结果与运行事件 11 Response.Write(string.Format("开始调用时间:{0}<Br>", DateTime.Now.ToString("mm:ss.ffff")));//开始调用事件 12 localService.HelloWorldAsync();//异步调用 13 Response.Write(string.Format("调用结束时间:{0}<Br>", DateTime.Now.ToString("mm:ss.ffff"))); 14 Thread.Sleep(4000);//等待异步操作完成 15 }
2、
1 /// <summary> 2 /// 使用 Begin/End 模式的回调技术 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 protected void BeginEndCallBackButton_Click(object sender, EventArgs e) 7 { 8 Response.Write("Begin/End 模式的回调技术<BR>"); 9 LocalService.WebService localService = new LocalService.WebService(); //创建代理类对象。 10 Response.Write(string.Format("开始调用时间:{0}<Br>", DateTime.Now.ToString("mm:ss.ffff")));//开始调用事件 11 IAsyncResult ar = localService.BeginHelloWorld(helloWorldCallBack, localService); 12 Response.Write(string.Format("调用结束时间:{0}<Br>", DateTime.Now.ToString("mm:ss.ffff"))); 13 while (!ar.IsCompleted) { }//空循环等待完成 14 } 15 private void helloWorldCallBack(IAsyncResult ar) 16 { 17 LocalService.WebService ws = (LocalService.WebService)ar.AsyncState; 18 string result= ws.EndHelloWorld(ar); 19 Response.Write(string.Format("完成时间:{0}。 结果{1}<Br>", DateTime.Now.ToString("mm:ss.ffff"), result)); 20 } 21 }