共用方式為


HOW TO:使用等候技術實作非同步 Web 服務用戶端

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

等候技術是實作 Web 服務用戶端與 Web 服務方法進行非同步通訊 (儘管這個方法可能是要用於同步存取) 的一種方式。在<以非同步方式與 XML Web Service 通訊>主題中會說明這個技術。

這個範例是以具有 Factorize 方法的 Web 服務類別 PrimeFactorizer 為基礎,而 Wsdl.exe 工具已為此方法產生了兩個非同步用戶端 Proxy 方法 BeginFactorizeEndFactorize

若要實作等候技術

  1. Web 服務用戶端會呼叫所產生 Proxy 類別的 Begin 方法。

    IAsyncResult ar = pf.BeginFactorize(factorizableNum, null, null);
    
    Dim ar As IAsyncResult = pf.BeginFactorize(factorizableNum, _
        Nothing, Nothing)
    
  2. Web 服務用戶端會透過所傳回 IAsyncResultAsyncWaitHandle 屬性,來存取 WaitHandle 物件。用戶端會呼叫 WaitHandle 類別其中一個等候方法,並會等候一或多個同步物件收到信號。

    如果用戶端使用這個技術,以非同步方式只呼叫一個 Web 服務方法,則可以呼叫 WaitOne,等候該方法完成處理。其他等候方法為 WaitAnyWaitAll

    ar.AsyncWaitHandle.WaitOne();
    
    ar.AsyncWaitHandle.WaitOne()
    
  3. 當等候方法傳回時,用戶端會呼叫 End 方法取得結果。

    results = pf.EndFactorize(ar);
    
    results = pf.EndFactorize(ar)
    

範例

// -----------------------------------------------------------------------// Async Variation 2.
// Asynchronously invoke the Factorize method, 
//without specifying a call back.
using System;
using System.Runtime.Remoting.Messaging;
// MyFactorize, is the name of the namespace in which the proxy class is
// a member of for this sample.
using MyFactorize;  

class TestCallback
 {          
      public static void Main(){
            long factorizableNum = 12345;
            PrimeFactorizer pf = new PrimeFactorizer();

          // Begin the Async call to Factorize.
            IAsyncResult ar = pf.BeginFactorize(factorizableNum, null, null);

          // Wait for the asynchronous operation to complete.
          ar.AsyncWaitHandle.WaitOne();

          // Get the completed results.
          long[] results;     
          results = pf.EndFactorize(ar);
          
          //Output the results.
            Console.Write("12345 factors into: ");
            int j;
            for (j = 0; j<results.Length;j++){
                  if (j == results.Length - 1)
                      Console.WriteLine(results[j]);
                  else 
                      Console.Write(results[j] + ", ");
            }
        }
}
Imports System
Imports System.Runtime.Remoting.Messaging
Imports MyFactorize     ' Proxy class namespace

Public Class TestCallback
      Public Shared Sub Main()
            Dim factorizableNum As Long = 12345
            Dim pf As PrimeFactorizer = new PrimeFactorizer()

          ' Begin the Async call to Factorize.
            Dim ar As IAsyncResult = pf.BeginFactorize(factorizableNum, Nothing, Nothing)

          ' Wait for the asynchronous operation to complete.
          ar.AsyncWaitHandle.WaitOne()

          ' Get the completed results.
          Dim results() as Long
          results = pf.EndFactorize(ar)
          
          'Output the results.
            Console.Write("12345 factors into: ")
            Dim j as Integer
            For j = 0 To results.Length - 1
                  If  j = (results.Length - 1) Then
                      Console.WriteLine(results(j) )
                  Else 
                      Console.Write(results(j).ToString + ", ")
                    End If
          Next j         
      End Sub
End Class

另請參閱

工作

HOW TO:使用回呼技術實作非同步 Web 服務用戶端
HOW TO:從 Web 服務用戶端發出非同步呼叫

概念

以非同步方式與 XML Web Service 通訊
建置 XML Web Service 用戶端

其他資源

建立 XML Web Service 的用戶端