Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
New Feature: Community Sites:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
|
Asynchronous Web Service in .NET
Posted Date: 06 Oct 2008 Resource Type: Articles Category: WCF/Webservices
|
Posted By: VijayaShankar Member Level: Gold Rating: Points: 20
|
Asynchronous Web Service in .NET
By default, when an ASP.NET Web Forms or a Windows Forms application calls a Web Service, it makes a synchronous call, meaning that the application comes to a grinding halt while it waits for a response from the Web Service. This process can definitely affect the performance of your applications, especially when calling several Web Services. However, you could potentially enjoy performance gains by calling the Web Service, and then continuing to address other business logic or processing needs, such as giving the user control of the application. When the Web Service response is returned, the application then responds accordingly. This process is referred to as calling Web Services asynchronously
How Asynchronous Calls Work How do you build an asynchronous Web Service? You don’t. That’s right—there is nothing special you have to do to create a Web Service that can be called asynchronously. Actually, when you create a Web reference in your consuming application, a proxy is created for the call to the Web Service. If you’ve cracked open the proxy code and looked at it, you’ll notice an additional method that enables you to issue a call, but not wait for its response (Begin), and another method that allows you to return to the response and retrieve the results (End). So there are no asynchronous Web Services - just asynchronous calls to Web Services. It can be achieved by two ways 1. Callback 2. watihandle
Callbacks
A callback is a function that waits in readiness to process the response from the Web Service. A reference to that function is passed into the initial call to the Web Service’s Begin method and is managed by the AsyncCallback object. This object knows how to reference a function designated as the callback function and coordinate that information with the IAsyncResult object, which knows how to take the incoming results and route them to the AsyncCallback’s function. The callback function has a reference to the instance of the IAsyncResult object containing information on the specific results from a specific call, and can use properties from it to retrieve a reference back to the AsyncCallback object. From here, the End method is called and the results can be parsed. The benefit of callbacks is that they enable you to return control of the application to the user. When the function that initially makes the call goes out of scope, the user can then continue to interact with the application. When the results are returned to the AsyncCallback’s function, the application briefly takes over and refreshes the screen with the new data. Therefore, callbacks are ideal when you want quick response on a Windows Forms application.
Understanding WaitHandle The IAsyncResult interface implements the functionality that holds a pointer to a callback function, which runs when the Web Service returns with the response. Alternatively, the WaitHandle function waits for the response to finish before it allows processing to continue. Calls to the Web Service can be made and other processing can continue, but ultimately the function handles processing the results of the Web Service on its own without the aid of an additional function.
Although WaitHandle can definitely be used by Windows Forms applications (as the following example in this chapter attests), it is a better fit for ASP.NET Web Forms that need to finish all processing before sending a completely processed page back to the user’s browser.
There are three varieties of WaitHandle functions: 1. wait all This method waits until all Web Services have finished before allowing the function to continue processing. 2. wait any This method waits until the first (of potentially many) Web Service returns a result before allowing the function to continue processing 3. wait one This method is the equivalent of callbacks in that it is concerned with only one Web Service, not multiples like the other WaitHandle methods.
Sample Code: Web Service
using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols;
/// /// Summary description for AsynWebService /// [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class AsynWebService : System.Web.Services.WebService {
public AsynWebService () {
//Uncomment the following line if using designed components //InitializeComponent(); }
[WebMethod] public string HelloWorld() { return "Hello World"; }
[WebMethod] public string AsynCallWait(int sleepTime) { System.Threading.Thread.Sleep(sleepTime); return sleepTime + " Task Finished"; } }
client Code
private void button1_Click(object sender, System.EventArgs e) { localhost.SleepService1 cService = new localhost.SleepService1(); // cb is a function pointer to ServiceCallback AsyncCallback cb = new AsyncCallback(ServiceCallback); // Call the Begin method of the proxy class to initiate the // asynchronous call to the Web Service method. cService.BeginSleepServiceA(int.Parse(txtA.Text), cb, cService); } public void ServiceCallback(IAsyncResult ar) { // Retrieve the original state for the proxy localhost.SleepService1 cService = (localhost.SleepService1)ar.AsyncState; // Retrieve the results by calling the End method of the proxy class txtResult.Text = cService.EndSleepServiceA(ar); }
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|