HOW TO:建立非同步 Web 服務方法

本主題專門說明舊有技術。 應該使用下列建立 XML Web Service 及 XML Web Service 用戶端: Windows Communication Foundation.

這個程序描述如何將 Web 服務方法轉換成一對專為非同步存取所設計的方法。它遵循 .NET Framework 非同步設計模式。非同步 XML Web Service 方法主題會說明這個程序的運作方式,以及 Wsdl.exe 工具如何產生可非同步存取 Web 服務方法的用戶端 Proxy 類別 (即使這些 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

另請參閱

工作

HOW TO:鏈結非同步呼叫與 Web 服務方法

概念

非同步 XML Web Service 方法
以非同步方式與 XML Web Service 通訊