方法 : 待機手法を使用して非同期 Web サービス クライアントを実装する

このトピックの対象は、レガシ テクノロジに特定されています。XML Web サービスと XML Web サービス クライアントは以下を使用して作成してください。 Windows Communication Foundation.

待機手法は、Web サービス メソッドが同期アクセスを目的として作成されている場合でも、Web サービス メソッドと非同期通信を行う Web サービス クライアントを実装する方法の 1 つです。この手法については、トピック「XML Web サービスとの非同期通信」を参照してください。

次の例は、Factorize メソッドを持つ PrimeFactorizer Web サービス クラスに基づいています。これに対して、Wsdl.exe ツールによって 2 つの非同期クライアント プロキシ メソッド (BeginFactorize および EndFactorize) が生成されています。

待機手法を実装するには

  1. Web サービス クライアントは、生成されたプロキシ クラスの Begin メソッドを呼び出します。

    IAsyncResult ar = pf.BeginFactorize(factorizableNum, null, null);
    
    Dim ar As IAsyncResult = pf.BeginFactorize(factorizableNum, _
        Nothing, Nothing)
    
  2. Web サービス クライアントは、返された IAsyncResultAsyncWaitHandle プロパティを介して、WaitHandle オブジェクトにアクセスします。クライアントは、WaitHandle クラスの待機メソッドのいずれかを呼び出し、1 つ以上の同期オブジェクトがシグナル状態になるまで待機します。

    クライアントは、この手法を使用して Web サービス メソッドを 1 つだけ非同期に呼び出す場合、WaitOne を呼び出してそのメソッドの処理が完了するまで待機できます。その他の待機メソッドとしては、WaitAny および WaitAll があります。

    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

参照

処理手順

方法 : コールバック手法を使用して非同期 Web サービス クライアントを実装する
方法 : Web サービス クライアントから非同期呼び出しを行う

概念

XML Web サービスとの非同期通信
XML Web サービス クライアントの作成

その他のリソース

XML Web サービスのクライアントの作成