如何:创建异步 Web 服务方法

本主题专门介绍一项旧有技术。现在应通过使用以下链接来创建 XML Web 服务和 XML Web 服务客户端: Windows Communication Foundation.

此过程描述如何将 Web 服务方法转换为一对专门用于异步访问的方法。它遵从 .NET Framework 异步设计模式。主题异步 XML Web services 方法介绍此过程的工作原理,同时说明 Wsdl.exe 工具如何生成能够异步访问 Web 服务方法的客户端代理类(即使它们是专门为同步访问而设计的也不例外)。

实现异步 Web 服务方法

  1. 将同步 Web 服务方法拆分为两个方法,让每个方法都具有相同的基名称,并使其中一个以 Begin 开头,另一个以 End 开头。

  2. Begin 方法的参数列表包含方法功能的 in 和 by reference 参数,另外还有两个其他参数。

    • by reference 参数以 in 参数的形式列出。

    • 倒数第二个参数必须为 AsyncCallbackAsyncCallback 参数允许客户端提供委托,以便在方法完成后加以调用。当一个异步 Web 服务方法调用另一个异步方法时,可以将此参数传入该方法的倒数第二个参数。

    • 最后一个参数是 ObjectObject 参数允许调用方为方法提供状态信息。当一个异步 Web 服务方法调用另一个异步方法时,可以将此参数传入该方法的最后一个参数。

    • 返回值必须属于 IAsyncResult 类型。

  3. End 方法的参数列表包含 IAsyncResult,它后面是特定于方法功能的任何 outby reference 参数。

    • 该返回值与同步 Web 服务方法的返回值属于同一类型。

    • by reference 参数以 out 参数的形式列出。

示例

using System;
using System.Web.Services;

[WebService(Namespace="https://www.contoso.com/")]
public class MyService : WebService 
{
    public RemoteService remoteService;
    public MyService() 
    {
        // Create a new instance of proxy class for 
        // the 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(IAsyncResult
        asyncResult) 
    {
        // Return the asynchronous result from the other Web service.
        return remoteService.EndReturnedStronglyTypedDS(asyncResult);
    }
}
Imports System.Web.Services
<WebService(Namespace:="https://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 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 Web service.
        Return remoteService.EndReturnedStronglyTypedDS(asyncResult)
    End Function
End Class

另请参见

任务

如何:用 Web 服务方法链接异步调用

概念

异步 XML Web services 方法
与 XML Web services 进行异步通信