Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : .NET : Web Services : Professional ASP.NET Web Services : Asynchronous Programming
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed
Asynchronous Process
Asynchronous Process

The procedure for making an asynchronous call is not as simple as its synchronous counterpart:

  1. The client obtains an interface pointer to the server object and calls the method asynchronously. The client includes a function pointer to a sink object for message callback.
  2. The call returns immediately and the calling thread is free to execute the next line of code.
  3. When the method is finished processing the request, the server notifies the client through the callback routine in the sink object.

Even with advancements implemented in the .NET framework, successfully developing asynchronous programming logic is not trivial. You need to examine the requirements of your application carefully to determine whether or not the code you're writing can even benefit from asynchronous events. Here are some general guidelines to consider when making your decision:

  • Consider asynchronous processing if the calling thread controls a Windows user interface. In this case, the calling thread can't afford to be blocked during a remote method call because the UI will freeze.
  • Asynchronous processing may help with scalability if the Web Services client is an ASP.NET application or another ASP.NET Web Service. In this scenario, a blocked synchronous call in the code can stall the ASP.NET worker thread, which can force other applications' requests to queue and, therefore, impact scalability. Using asynchronous communication instead could at least free up the threads that ASP.NET isn't using for the Web Service calls. Asynchronous server processing is discussed in detail towards the end of this chapter.
  • If there is a possibility that the remote procedure call to the Web Service may take a while to complete, asynchronous processing may be beneficial. In this case the client application can do other work on the thread before it needs the results from the remote procedure call.
  • The client application may need to make concurrent calls to one or more remote services. In this case, using an asynchronous remote procedure call is much better than spinning off multiple threads to do the same work. For example, if an application needs to make concurrent synchronous calls to three different Web Services, it cannot do so with one thread. It has to spin off at least two threads and make a remote call in each thread. However, if the client uses an asynchronous remote call, it can make all three calls on one thread and then wait for all of them.

A Sample Web Service :
Now that we've discussed the pros and cons of both the synchronous and asynchronous programming methodologies, let's write some code to illustrate the concepts. We'll begin by creating an ASP.NET Web Service that our client application can invoke. We'll then create two separate applications, with one calling the Web Service synchronously, and the other asynchronously, so that we can compare the techniques of each approach.

For the purposes of our discussion, we will be making use of an ASP.NET Web Service that returns a stock quote. The Web Service, named StockService, accepts a ticker symbol parameter that will return a string representing the value of the stock. In order to properly demonstrate asynchronous programming, the Web Service we will be invoking will also have the ability to simulate a long-running process. For that purpose, the StockService example accepts another parameter that represents the number of seconds the service will wait before returning the stock value back to the calling application. opposite is a screenshot of the StockService.asmx page displaying the GetStockQuote method.

StockService.asmx
StockService.asmx

The TickerSymbol parameter will accept a string value representing a stock symbol or a company name. The DelayInSeconds parameter will accept an integer value representing the number of seconds the Web Service will wait before returning the stock's value. Below is the code for the StockService Web Service written in C#. Bear in mind that this Web Service is simply a simulation for illustrative purposes only and obviously doesn't return accurate stock values. The actual algorithm that a production Web Service would use to return a stock's actual value will be much more complex than this example.

StockService.asmx :

<%@ WebService Language="C#" Class="StockWebService.StockService" %>

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Threading;

namespace StockWebService {

  public class StockService :  System.Web.Services.WebService
  {
    [WebMethod(Description="Returns  a stock  quote")]
    public string GetStockQuote(string TickerSymbol, int DelayInSeconds)
    {
      //Create a delay to  simulate a long-running  process
      if (DelayInSeconds >  0)
      {
        //Have the thread sleep  based on the DelayInSeconds  parameter
        //Note:  The  constant "1000"  is to convert seconds to milliseconds
        System.Threading.Thread.Sleep(DelayInSeconds * 1000);
      }

      //Retrieve the stock quote based on  the  TickerSymbol parameter
      //NOTE:  Stock values are for simulation  purposes only
      string Quote;
      switch (TickerSymbol.ToUpper())
      {
        case "MSFT":
          Quote =  "67";
          break;
        case "SUNW":
          Quote =  "36  31/32";
          break;
        case "IBM":
          Quote =  "80";
          break;
        case "ORCL":
          Quote =  "25  1/32";
          break;
        case "CSCO":
          Quote =  "51";
          break;
        default:
          Quote =  "Unknown";
          break;
      }
      
      //Return value of Quote  to calling application
      return Quote;
    }
  }
}

The Web Service has one method, GetStockQuote. GetStockQuote accepts the two parameters we previously discussed: TickerSymbol and DelayInSeconds.

[WebMethod(Description="Returns a stock quote")]
public string GetStockQuote(string TickerSymbol, int DelayInSeconds)

The GetStockQuote method returns a string, which represents the value of the requested stock. The method will return a string value of "Unknown" if the stock's value is not known.

Additionally, this method has the functionality to delay the delivery of the return message to simulate a long server-side process. The code accomplishes this by retrieving a handle to the current thread and causes the thread to sleep for the duration specified in the DelayInSeconds parameter:

	//Have the thread sleep based on the DelayInSeconds parameter
	//Note: The constant "1000" is to convert seconds to milliseconds
	System.Threading.Thread.Sleep(DelayInSeconds * 1000);

Here is the output of the GetStockQuote method when invoked from the sample page provided by ASP.NET:

Stock Service
Stock Service

Previous ( 1 Gone )( 6 Remaining ) Next

Comments/Questions ( Threads: 3, Comments: 3 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Handle Client applications asynchronously using a Web Service
  2. Getting some problems

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.