Asynchronous Web Methods ASP.NET

By default,all web methods declared on a web service are synchronous on the server side.However,the call from the browser via XML HTTP is asynchronous, but the actual execution of the web method at the server is synchronous.

This means that from the moment a request comes in to the moment the response is generated from that web method call, it occupies a thread from the ASP.NET worker pool. If it takes a relatively long period of time for a request to complete, then the thread that is processing the request will be in use until the method call is done. Unfortunately, most lengthy calls are due to something like a long data base query or perhaps a call to another web service. For instance, if you make a database call, the current thread waits for the database call to complete.

The thread simply has to wait around doing nothing until it hears back from its query.Similar issues arise when a thread waits for a call to a TCP socket or a backend web service to complete.

When you write a typical ASP.NET web service using web methods, the compiler compiles your code to create the assembly that will be called when requests for its web methods are received.When your application is first launched, the ASMX handler reflects over the assembly to determine which web methods are exposed.

For normal synchronous requests, it is simply a matter of finding which methods have a [Web Method] attribute associated with them.

To make asynchronous web methods, you need to ensure the following rules are met:

  • There is a BeginXXX and EndXXX web method where XXX is any string that represents the name of the method you want to expose.
  • The BeginXXX function returns an IAsyncResult interface and takes an AsyncCallback and an object as its last two input parameters,respectively.
  • The EndXXX function takes an IAsyncResult interface as its only parameter.
  • Both the BeginXXX and EndXXX methods must be flagged with the Web Method attribute.

If the ASMX handler finds two methods that meet all these requirements, then it will expose the XXX method in its WSDL as if it were a normal web method.

Example shows a typical synchronous web method and Example shows how it is made asynchronous by introducing a Begin and End pair.

Example of a synchronous web method

The ASMX handler will expose a web method named Sleep from the pair of web methods.The method will accept the parameters defined before the AsyncCallback parameter in the signature for BeginXXX as input and return with the EndXXX function.

After the ASMX handler reflects on the compiled assembly and detects an asynchronous web method, it must handle requests for that method differently than it handles synchronous requests. Instead of calling the Sleep method synchronously andproducing responses from the return value,it calls the BeginSleep method. It deserializes the incoming request into the parameters to be passed to the function—as it does for synchronous requests—but it also passes the pointer to an internal callback function as the extra AsyncCallback parameter to the BeginSleep method.

After the ASMX handler calls the BeginSleep function, it will return the thread to the process thread pool so it can handle another request.The HttpContext for the request will not be released yet.The ASMX handler will wait until the callback function that it passed to the BeginSleep function is called to finish processing the request.

Once the callback function is called, a thread from the thread pool is taken out to execute the remaining work.The ASMX handler will call the EndSleep function so that it can complete any processing it needs to perform and return the data to be rendered as a response.Once the response is sent, the HttpContext is released (see Figure).

How the asynchronous web method works

How the asynchronous web method works

The asynchronous web method concept is hard to grasp.It does not match with anything that we do in regular development. There are some fundamental differences and limitations to consider:

  • You cannot use asynchronous web methods when you use a business layer to read or write data that’s not asynchronous itself.For example,a web method calling some function on Dash board Facade will not benefit from an asynchronous approach.
  • You cannot use the asynchronous method when you are calling an external web service synchronously.The external call must be asynchronous.
  • You cannot use the asynchronous method when you perform database operations using regular synchronous methods.All database operations must be asynchronous.
  • There’s no benefit in making an asynchronous web method when there’s no wait on some I/O operation such as HTTP requests, web service calls, remoting, asynchronous data base operations,or asynchronous file operations.You won’ t benefit from simple Delegate.Begin invoke calls, which run a function asynchronously,because asynchronous delegates take threads from the same thread pool as ASP.NET

So,in Example,neither the simple sleep function nor any of the methods that we have used in our proxy web service can be real asynchronous functions(see Chapter).We need to rewrite them to support the asynchronous call nature. Before we do so,remember one principle—you can only benefit from the asynchronous method when the BeginXXX web method ends up calling a BeginYYY method on some other component, and your EndXXX method calls that component’s EndYYY method.Otherwise, there’s no benefit in making web methods asynchronous

Example shows the code for a simple stock quote proxy web service.The proxy web service’s Begin Get Stock method ends up calling the Begin GetS tock method on a component that fetches the stock data from external source. When data arrives,the component calls back via the Async Call back cb.The ASMX handler passes down this call back to the web method.So,when it is called, ASP NET’s ASMX handler receives the call back, and it restores the Http Context,calls End Get Stock, and renders the response.

Example of a stock quote proxy web service

Example of a stock quote proxy web service (continued)

The problem is ASP.NET’s ASMX handler has the capability to call asynchronous web methods and return threads to the ASP.NET thread pool, but ASP.NET AJAX Framework’s ASMX handler does not have that capability. It supports only synchronous calls.So, we need to rewrite the ASMX handler of ASP.NET AJAX to support asynchronous web method execution and then bypass ASP.NET AJAX’s ASMX handler when web methods are called via XML HTTP.In the next section, you will seem how the ASP.NET AJAX Frame work’s ASMX handler works and how you can rewrite such a handler your self and introduce new features to it.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

ASP.NET Topics