Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

Optimizing Web service calls from Web forms

How to speed up calls to Web services from Web forms.

In the previous tip, I demonstrated how to call Web services asynchronously from a Win Form application using callbacks. This technique allows the user to continue to interact with the client application while waiting for the Web service to return its results. Once the Web service values have been returned, the application calls the callback handler, a function that is designated to handle the results of the Web service. The callback handler can refresh the application and the user's interaction with the application is never interrupted.

While this approach works well with Win Forms, it is not a good technique to use for Web Forms. Imagine trying to use callbacks with a Web Form application. Your ASP.NET code would make a call to a Web service. The Web page is then rendered and sent to the client's Web browser. But how would you notify the browser that the results have finally returned? You can't, due to the request/response nature of the Web. Instead of using the callback approach, you'll use the WaitHandle object technique. Basically, this entails making a call to the Web service as early as possible in your ASP.NET code, then to perform as much other processing (i.e., component or database calls, calculations, etc.) as possible before waiting for the result of the Web service. So there is still a blocking issue if your processing completes before the Web service result is returned, although its negative impact is somewhat diminished.

The .NET Framework provides several methods on the WaitHandle object for asynchronous calls to accommodate different scenarios as follows:

WaitOne -- Used when you are only calling one Web service to render a given Web Form. When this single Web service is called, processing can be resumed. At the last possible moment, the WaitOne() method is called which essentially waits until the result comes back from the Web service until the ASP.NET page can finish processing.

WaitAll -- Used when you are calling multiple Web services to render a given Web Form AND the results from all Web services must come back before page processing can resume. In other words, if you call three Web services, processing can continue until the last possible moment. When you use the WaitAll method, all three Web services must finish (i.e., return their results back to the consumer) before the ASP.NET page can finish processing.

WaitAny -- Used when you are calling multiple Web services to render a given Web Form AND the results from any single Web service before page processing can resume. In other words, if you call three Web services, processing can continue until the last possible moment when you must call the WaitAny method. At that point, any one of the three Web services can complete and allow the ASP.NET page to finish processing.

In this first snippet, a single button (Button1) is on the Web Form. When clicked, a single Web service is invoked. The page can continue to process until the last possible moment. At that point, the WaitOne method must wait for the result to come. Then, the result is written to the page and that is rendered back to the client.

private void Button1_Click(object sender, System.EventArgs e)
    localhost.CustomerSales wsCustomerSales = 
        new localhost.CustomerSales();

    IAsyncResult ar = 
        wsCustomerSales.BeginGetCustomerSalesInformation(null, null);

    // Perform as much processing as possible here!



I won't illustrate the use of WaitAll in this article, but look for it in the source code that I've provided for this tip. Instead, I chose to illustrate the use of the WaitAny because we will use the IsCompleted property of each instance of the IAsyncResult object for the two Web Services that are called. When a user clicks on the Button2, the code behind calls two Web services. The first one that returns a result will display a message. The other Web service will be ignored.

private void Button2_Click(object sender, System.EventArgs e)
      // Create instances of the Web Services
      localhost1.CustomerPayment wsCustomerPayment = 
        new localhost1.CustomerPayment();
      localhost.CustomerSales wsCustomerSales = 
        new localhost.CustomerSales();

      // Create async result objects
      IAsyncResult arPayment =   wsCustomerPayment.BeginGetCustomerPaymentInfo(null, null);
      IAsyncResult arSales = wsCustomerSales.BeginGetCustomerSalesInformation(null, null);

      // Create an array of WaitHandle objects
      WaitHandle[] wh = 
        {arPayment.AsyncWaitHandle, arSales.AsyncWaitHandle};

      // Perform as much processing as possible here!
      int iReturn = WaitHandle.WaitAny(wh);

      if (arPayment.IsCompleted)
        int iResult = wsCustomerPayment.EndGetCustomerPaymentInfo(arPayment);
        Response.Write("Payment finished first: " + iResult);
      else if (arSales.IsCompleted)
      int iResult = wsCustomerSales.EndGetCustomerSalesInformation(arSales);
      Response.Write("Sales finished first: " + iResult );

In conclusion, using the correct asynchronous call technique for the type of application you are building will page huge dividends on performance and customer satisfaction.

About the Author

Robert Tabor is a Microsoft Certified Professional in Visual Basic with over six years of experience developing n-tier Microsoft-centric applications for some of the world's most prestigious companies and consulting organizations, such as Ernst & Young, KPMG, Cambridge Technology Partners, Sprint, American Heart Association, and the Mary Kay Corporation. Bob is the author of Microsoft .NET XML Web Services by Sams Publishing, and contributes to SoapWebServices.com and LearnVisualStudio.NET. He is currently working on initiatives within Mary Kay, the second largest eCommerce site in retail volume on the net, of how to utilize .NET within their e-business group.

Dig Deeper on Topics Archive

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.