Web Services (ASMX 1.1) Performance Guidelines - Asynchronous Invocation

From Guidance Share

Jump to: navigation, search

- J.D. Meier, Srinath Vasireddy, Ashish Babbar, and Alex Mackman


Consider Calling Web Services Asynchronously When You Have Additional Parallel Work

Asynchronous invocation is the most useful when the client has additional work that it can perform while the Web method executes. Asynchronous calls to Web services result in performance and throughput gains because you free the executing worker thread to do parallel work before it is blocked by the Web services call and waits for the results. This lets you concurrently process any work that is not dependent on the results of the Web services call. The following code snippet shows the approach.

  private void Page_Load(object sender, System.EventArgs e) 
  {
    serv = new localhost.WebService1();
    IAsyncResult result = serv.BeginLengthyProcedure(5000,null,null);
    // perform some additional processing here before blocking 
    // wait for the asynchronous operation to complete
    result.AsyncWaitHandle.WaitOne();
    string retStr = serv.EndLengthyProcedure(result);
  }


Use Asynchronous Invocation to Call Multiple Unrelated Web Services

Consider asynchronous invocation if you need to call multiple Web services that do not depend on each other's results. Asynchronous invocation lets you call the services concurrently. This tends to reduce response time and improve throughput. The following code snippet shows the approach.


  private void Page_Load(object sender, System.EventArgs e){
    serv1 = new WebService1();
    serv2 = new WebService2();
    IAsyncResult result1 = serv1.BeginLengthyProcedure(1000,null,null);
    IAsyncResult result2 = serv2.BeginSlowProcedure(1000,null,null);
    //wait for the asynchronous operation to complete
    WaitHandle[] waitHandles = new WaitHandle[2];
    waitHandles[0] = result1.AsyncWaitHandle;
    waitHandles[1] = result2.AsyncWaitHandle;
    WaitHandle.WaitAll(waitHandles);  //depending upon the scenario you can
                                   //choose between WaitAny and WaitAll
    string retStr1 = serv1.EndLengthyProcedure(result1);
    string retStr2 = serv2.EndSlowProcedure(result2);
  }


Call Web Services Asynchronously for UI Responsiveness

By calling a Web service asynchronously from a Windows Forms application, you free the main user interface thread. You can also consider displaying a progress bar while the call progresses. This helps improve perceived performance.

However, you need to perform some additional work to resynchronize the results with the user interface thread because the Web service call is handled by a separate thread. You need to call the Invoke method for the control on which you need to display the results.

References

For more information, see the MSDN article, "At Your Service: Performance Considerations for Making Web Service Calls from ASPX Pages," at http://msdn.microsoft.com/library/en-us/dnservice/html/service07222003.asp.

Personal tools