Home > Microsoft .Net Development Tips > ASP.NET Development > Optimizing Web service calls from Web Forms
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ASP.NET DEVELOPMENT

Optimizing Web service calls from Web Forms


Bob Tabor
10.17.2002
Rating: -5.00- (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 SearchWinDevelopment.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
ASP.NET Development
How to use jQuery to solve Javascript browser compatibility problems
How to write an out-of-browser Silverlight 3 application in 3 steps
Silverlight 3 beta SDK download lets developers try new RIA features
Visual Studio's IntelliSense for jQuery doesn't autocomplete correctly
Dundas Map for .NET kicks up geographic visualization
Return to CodePlex: Into the Sandcastle…
VBScript Tutorial
Use PHP with Visual Studio to create Web sites
Visual Studio Team System Add-ins: Conchango Scrum for Team System and Scrum Dashboard
Visual Studio 2008 and .NET Framework 3.5 SP1 introduces ADO .NET Entity Designer

Web services and SOA implementations in the .NET Framework
Goin' mobile with Windows
Microsoft "WebSphere Loves Windows" ads tout cost, performance vs. AIX
Microsoft releases new CTP of Oslo SDK
Microsoft's Oslo modeling platform, the M language and .NET
Outgoing Bill Gates says UML on tap in Oslo SOA modeler
Podcast: Windows CardSpace authors speak
Open XML SDK ready for the road
Some of the Zen of Volta
Scaling WCF applications can challenge development teams
Security interoperability with .NET/WSE and WebLogic Workshop 8.1

ASP.NET development best practices
Introduction to ASP.NET's Model View Controller (MVC) Design Pattern
Silverlight, Ajax components require different approach to UI
LINQ Learning Guide: LINQ and Web applications
VB code download home page
VB code: File manipulation downloads
Localization practices for .NET 2.0: It's still about the architecture
Creating custom ASP.NET 2.0 profile providers
.NET development in the trenches
Microsoft developers balancing age-old issues, barrage of new technology
SearchVB.com's Podcast Page

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
scripting language  (SearchWinDevelopment.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.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
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 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts