Home > SOA Tips > .NET Developer > Optimizing Web service calls from Web forms
SOA Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

.NET DEVELOPER

Optimizing Web service calls from Web forms


Bob Tabor
10.17.2002
Rating: -4.29- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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!

    ar.AsyncWaitHandle.WaitOne();
   Response.Write(wsCustomerSales.EndGetCustomerSalesInformation(ar).ToString());

  }

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.

Rate this Tip
To rate tips, you must be a member of SearchSOA.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Platforms and Servers
Tracking down managed memory leaks
Handling exceptions in .NET
.NET Compact Framework graphics
The Data Access Application Block
Decision time: .NET or J2EE?
A great .NET resource: .Net2TheMax
Delegates vs. interfaces in .NET
Project structure best practices
Working with PDFs in a .NET environment
Displaying errors with the error provider

Microsoft .NET Web services
Microsoft preps .NET 4.0 - framework improves on REST, MVC, JQuery support
How do I balance throughput requirements and interoperability?
APM software traces transactions across tiers, technologies
How you can learn M Grammar for Oslo modeling
Legacy modernization opens Windows for publisher
Former .NET Web developers ride Ruby and Rails application framework
Microsoft Oslo at PDC: Dial 'M' for modeling language
Yahoo proxy fight looms
New Microsoft site for architects
LAMP coders go hybrid route
Microsoft .NET Web services Research

.NET Developer
Programming Indigo
DataSets and Web services don't mix
Security in .NET 2.0
Tracking down managed memory leaks
Handling exceptions in .NET
.NET Compact Framework graphics
The Data Access Application Block
A great .NET resource: .Net2TheMax
Delegates vs. interfaces in .NET
Project structure best practices

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Common Language Infrastructure  (SearchSOA.com)
Visual J#  (SearchSOA.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SOA Trends and Strategy - SOA Education, SOA Development, SOA Implementations
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2001 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts