This documentation is archived and is not being maintained.

How to: Chain Asynchronous Calls with a Web Service Method

Visual Studio 2010

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 following code example demonstrates how to chain asynchronous calls when a Web service method makes more than one asynchronous call and the calls must execute sequentially. The BeginGetAuthorRoyalties method makes an asynchronous call to determine whether the author passed in is valid and sets up an intermediate callback named AuthorRoyaltiesCallback to receive the results. That intermediate callback then asynchronously calls to get the royalties for that author, if the author is valid.


using System.Web.Services;
using System.Data;
using System;
// This imports the proxy class for the Web services
// that the sample communicates with.
using AsyncWS.localhost;

namespace AsyncWS
    public class MyService : System.Web.Services.WebService
        public RemoteService remoteService;
        public MyService()
           remoteService = new RemoteService();

        public IAsyncResult BeginGetAuthorRoyalties(String Author,
               AsyncCallback callback, Object asyncState) 
          // Saves the current state for the call that gets the author's
          // royalties.
          AsyncStateChain state = new AsyncStateChain();
          state.originalState = asyncState;
          state.Author = Author;
          state.originalCallback = callback;

          // Creates an intermediary callback.
          AsyncCallback chainedCallback = new
          return remoteService.BeginGetAuthors(chainedCallback,state);
       // Intermediate method to handle chaining the 
       // asynchronous calls.
       public void AuthorRoyaltiesCallback(IAsyncResult ar)
          AsyncStateChain state = (AsyncStateChain)ar.AsyncState;
          RemoteService rs = new RemoteService();

          // Gets the result from the call to GetAuthors.
          Authors allAuthors = rs.EndGetAuthors(ar);

          Boolean found = false;
          // Verifies that the requested author is valid.
          int i = 0;
          DataRow row;
          while (i < allAuthors.authors.Rows.Count && !found)
             row = allAuthors.authors.Rows[i];
             if (row["au_lname"].ToString() == state.Author) 
                found = true;
          if (found)
             AsyncCallback cb = state.originalCallback;
             // Calls the second Web service, because the author is
             // valid.
            // Cannot throw the exception in this function or the XML Web
            // service will hang. So, set the state argument to the
            // exception and let the End method of the chained XML Web
            // service check for it.  
            ArgumentException ex = new ArgumentException(
              "Author does not exist.","Author");
            AsyncCallback cb = state.originalCallback;
            // Call the second Web service, setting the state to an
            // exception.

       public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResult
        // Check whehter the first Web service threw an exception.
        if (asyncResult.AsyncState is ArgumentException)
          throw (ArgumentException) asyncResult.AsyncState;
         return remoteService.EndReturnedStronglyTypedDS(asyncResult);
    // Class to wrap the callback and state for the intermediate
    // asynchronous operation.
    public class AsyncStateChain 
       public AsyncCallback originalCallback;
       public Object originalState;
       public String Author;

See Also