深入学习Web Service系列之异步开发模式
概述
在本篇随笔中,通过一些简单的示例来说一下Web Service中的异步调用模式。调用Web Service方法有两种方式,同步调用和异步调用。同步调用是程序继续执行前等候调用的完成,而异步调用在后台继续时,程序也继续执行,不必等待方法处理完成而直接返回。具体的调用流程见下图:
对于同步调用方法而言,UI线程依赖于方法的实现,方法执行时间过长将导致UI无法及时与用户进行交互。我们知道,在Windows客户端中,每个进程都有单一的UI进程,在服务器中,可扩展性依赖于线程的使用。对于异步调用方法而言,能够及时于用户交互响应,从而提供了良好的用户体验;同时也可以改善服务器的可扩展性,将服务器与通讯问题隔离。
客户端异步调用方法
在客户端异步调用是完全基于Proxy的方法,异步行为最简单的模式。Visual Studio和WSDL.EXE提供对它的直接支持。所以我们不必在Web服务应用程序中编写额外的代码来处理异步调用。
遍及.NET Framework的异步调用有一个基础的设计模式:Begin方法和End方法,他们分别用于开始和终止异步处理。Visual Studio和WSDL.exe生成了这两种方法:
Begin<WebServiceMethodName>——该方法通知Web服务开始处理调用,并立即返回。该方法不返回Web服务调用所指定的数据类型,而是返回一种实现IasyncResult接口的数据类型。
End<WebServiceMethodName>——该方法通知Web服务返回先前启动的Web方法所生成的结果。
IasyncResult接口包含了WaitHandle类型的AsyncWaitHandle特性。这个公共接口允许用户的客户应用程序等待调用,而且,该接口将用Any或All语义(例如WaitHandle.WaitOne,WaitAny和WaitAll)作为信号通知客户应用程序。例如,如果想要客户应用程序异步等候一个Web方法,可调用WaitOne来处理要完成的Web服务。
一般来说,客户端异步代理方法有两种实现机制:使用同步对象和回调机制(也许你可能对用这个词不习惯,实在找不到第二个词来代替,暂且这样称呼吧)
同步对象
同步对象允许用户对Web服务的方法进行调用(使用Begin方法),然后继续处理。在后面的程序中,可以调用End方法,传递同步对象,以便得到调用结果。这种方式下,能够继续执行函数中的程序流程,而不执行回调处理。在这里调用WaitOne()方法会挂起当前线程,避免忙等待的发生,直到Web Services方法调用结束返回后,该线程才会被重新唤起。
示例代码:
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
6
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
回调机制
从本质上说,异步回调机制是委托的.NET等价物,它通过在异步操作完成时建立一个被调用的单独方法来进行工作。调用应用程序能够继续处理其他的任务,直到回调函数被调用为止。这就意味着处理已经完成了,应用程序可以正常运行了。使用同步对象不同于回调机制的区别是,当检查Web方法是否已经完成,以及检查Web方法中是否含有需要的结果时,我们无法对其进行控制,而在回调的情况中,Web方法一旦完成,这些工作就会被自动执行。
示例代码:
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
6
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
10
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
13
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
17
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
19
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
23
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
25
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
使用回调机制还是同步对象取决于用户所面临的具体情况。在检查异步调用是否完成时,如果愿意对处理过程进行控制,那么可以选择使用同步对象。如果觉得自己编写代码来完成对Web服务的调用,且当方法一旦执行完毕就立即由所调用的特殊函数来处理所返回的结果更适合一些,那么就更适合用回调机制。
在客户端使用异步方法调用,可以改进UI响应度,在服务器端不需要实现异步操作,对服务器来说是透明的,而且客户端能够在任何时间选择阻塞。
服务端使用Soap One-Way方法
在服务器端使用One-Way方法实现异步调用,其实质是将单项消息发送到端点。这种方式的特点是方法没有返回值,客户端方法不会从调用的服务器端方法中收到返回值;我们无法判断方法结束的时间,对于结果需要显式通知或者轮询。
在Web 服务端,我们使用[SoapDocumentMethod]定义One-Way方法:
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
示例代码:
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
6
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
10
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
16
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
18
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
One-Way方法不适合于下列情况:
l 方法需要对结果轮询
l 方法需要同步
服务端使用WSE SoapSender和SoapRecevier
在进行本部分内容之前,我们需要安装WSE2.0。WSE支持面向消息的编程,为我们提供了SoapSender和SoapReceiver基类,它能够支持发送和接收SoapEnvelopes,同时它也通过SoapClient和SoapService提供了更多的事务支持。SoapSender和SoapReceiver在客户端和服务端同时实现,客户端使用SoapSender发送消息,同时可选择使用SoapReceiver接收消息;服务端使用SoapReceiver接收消息,同时也可以选择使用SoapSender发送通知和回应。
示例代码:
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
19
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
6
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
Web Service端:
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
7
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
8
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
22
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif)
25
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
26
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](https://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
服务端使用WSE 自定义SoapMSMQ传输
SoapMSMQ是一款开源软件,简化使用WSE进行MSMQ操作,下载地址:
http://www.codeproject.com/useritems/SoapMSMQ.asp
SoapMSMQ完全支持事务,具有如下特点:
l 在事务中,请求要被同步初始化
l 同步阶段排队请求,并且返回令牌
l 异步阶段处理各个事务
l 所有持有令牌的请求都保证会被处理,但可能会不成功
l 支持向客户端发送通知
对SoapMSMQ感兴趣的朋友可以下载下来后,做进一步的研究。
总结
异步方法调用改善了客户端的响应和用户体验,增加了服务端的可扩展性。当方法需要耗费大量的时间时,可以采用异步方式调用,提供系统并发处理的能力。对于异步方式的开发,我们可以有如上所述的广泛选择。
示例程序界面:
下载地址: