![]() ![]() |
|
ASP.NET创建Web服务之异步Web服务 | |
作者:佚名 文章来源:不详 点击数 更新时间:2008/4/5 9:39:11 文章录入:杜斌 责任编辑:杜斌 | |
|
|
通常,调用执行输入/输出操作的方法的XML Web服务方法适于作为异步实现。这样的方法的例子包括和其他的XML Web服务通讯、访问远程数据库、执行网络输入/输出和读写大文件方法。这些方法都花费大量时间执行硬件级操作,而把线程留着用来执行XML Web服务方法程序块。如果XML Web服务方法异步实现,那么线程可以被释放来执行其他的代码。 不管一个XML Web服务方法是否异步实现,客户端都可以与之异步通讯。使用Web服务描述语言工具(WSDL.EXE)生成的.net客户端中的代理类来实现异步通信,即使XML Web服务方法是同步实现。代理类包含用于与每个XML Web服务方法异步通信的Begin和End方法。因此,决定一个XML Web服务方法到底是异步还是同步要取决于性能。 注意:实现一个异步的XML Web服务方法对客户端和服务器上的XML Web服务之间的HTTP连接没有影响。HTTP连接既不不会关闭也不用连接池化。 实现一个异步的XML Web服务方法 实现一个异步的XML Web服务方法遵循NET Framework异步设计模式 把一个同步的XML Web服务方法分解为两个方法;其中每个都带有相同的基名--一个带有以Begin开头的名称,另一个带有以End开头的名称。 Begin方法的参数表包含方法的功能中的所有的in和by引用参数。 By引用参数是作为输入参数列出的。 倒数第二个参数必须是AsyncCallback。AsyncCallback参数允许客户端提供一个委托,在方法调用完成的时候调用。当一个异步XML Web服务方法调用另一个异步方法,这个参数可以被传入那个方法的倒数第二个参数。最后一个参数是一个对象。对象参数允许一个调用者提供状态信息给方法。当一个异步XML Web服务方法调用另一个异步方法,这个参数可以被传入那个方法的最后一个参数。 返回值必须是IAsyncResult类型的。 下面的代码示例是一个Begin方法,有一个方法函数特定的String参数。 [C#] [WebMethod] public IAsyncResult BeginGetAuthorRoyalties(String Author, AsyncCallback callback, object asyncState) [Visual Basic] <WebMethod()> _ Public Function BeginGetAuthorRoyalties(ByVal Author As String, _ ByVal callback As AsyncCallback, ByVal asyncState As Object) _ As IAsyncResult End方法的参数表由一个IAsyncResult类型的out和by引用参数组成。 返回值与一个同步的XML Web服务方法的返回值类型相同。 By引用参数是作为输出参数列出的。 下面的代码示例是一个End方法,返回一个AuthorRoyalties用户定义的模式。 [C#] [WebMethod] public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResult asyncResult) [Visual Basic] <WebMethod()> _ Public Function EndGetAuthorRoyalties(ByVal asyncResult As _ IAsyncResult) As AuthorRoyalties 下面的代码示例是一个和另一个XML Web服务方法异步通讯的异步XML Web服务方法。 [C#] using System; using System.Web.Services; [WebService(Namespace="http://www.contoso.com/")] public class MyService : WebService { public RemoteService remoteService; public MyService() { // Create a new instance of proxy class for // the XML Web Service to be called. remoteService = new RemoteService(); } // Define the Begin method. [WebMethod] public IAsyncResult BeginGetAuthorRoyalties(String Author,AsyncCallback callback, object asyncState) { // Begin asynchronous communictation with a different XML Web // service. return remoteService.BeginReturnedStronglyTypedDS(Author, callback,asyncState); } // Define the End method. [WebMethod] public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResultasyncResult) { // Return the asynchronous result from the other XML Web service. return remoteService.EndReturnedStronglyTypedDS(asyncResult); } } [Visual Basic] Imports System.Web.Services <WebService(Namespace:="http://www.contoso.com/")> _ Public Class MyService Inherits WebService Public remoteService As RemoteService Public Sub New() MyBase.New() ' Create a new instance of proxy class for ' the XML Web service to be called. remoteService = New RemoteService() End Sub ' Define the Begin method. <WebMethod()> _ Public Function BeginGetAuthorRoyalties(ByVal Author As String, _ ByVal callback As AsyncCallback, ByVal asyncState As Object) _ As IAsyncResult ' Begin asynchronous communictation with a different XML Web ' service. Return remoteService.BeginReturnedStronglyTypedDS(Author, _ callback, asyncState) End Function ' Define the End method. <WebMethod()> _ Public Function EndGetAuthorRoyalties(ByVal asyncResult As _ IAsyncResult) As AuthorRoyalties ' Return the asynchronous result from the other XML Web service. Return remoteService.EndReturnedStronglyTypedDS(asyncResult) End Function End Class |
|
![]() ![]() |