Export (0) Print
Expand All

How to: Implement an Asynchronous Web Service Client Using the Wait Technique

This topic is specific to a legacy technology. XML Web services and XML Web service clients should now be created using Windows Communication Foundation .

The wait technique is one way to implement a Web service client to communicate with a Web service method asynchronously, even though the method may be intended for synchronous access. The technique is explained in the topic Communicating with XML Web Services Asynchronously.

This example is based on a Web service class PrimeFactorizer with a method Factorize, for which the Wsdl.exe tool has generated two asynchronous client proxy methods, BeginFactorize and EndFactorize.

To implement the wait technique

  1. The Web service client calls the Begin method of the generated proxy class.

    IAsyncResult ar = pf.BeginFactorize(factorizableNum, null, null);
    

    Dim ar As IAsyncResult = pf.BeginFactorize(factorizableNum, _
        Nothing, Nothing)
    
  2. The Web service client accesses a WaitHandle object through the AsyncWaitHandle property of the returned IAsyncResult. The client calls one of the WaitHandle class's wait methods, and waits for one or more synchronization objects to be signaled.

    If a client uses this technique to asynchronously call only one Web service method, it can call WaitOne to wait for the processing of that method to complete. Other wait methods are WaitAny and WaitAll.

    ar.AsyncWaitHandle.WaitOne();
    

    ar.AsyncWaitHandle.WaitOne()
    
  3. When the wait method returns, the client calls the End method to get the results.

    results = pf.EndFactorize(ar);
    

    results = pf.EndFactorize(ar)
    

Example

// -----------------------------------------------------------------------// 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] + ", ");
            }
        }
}

See Also

Community Additions

ADD
Show:
© 2014 Microsoft