Server-side asynchronous web method
Matt PowellMicrosoft Corporation October 2, 2002
Summary: Matt Powell describes how to create high-performance Microsoft ASP.NET Web services on the server side to create high-performance Microsoft ASP.NET Web services.
Introduction
In the third column (English) in September (English), I talked about the issue of using Microsoft® .NET Framework's client feature through HTTP asynchronous calling Web services. This method of calling a web service is very useful. You do not have to lock your app or generate too many background threads when you use. Now let's take a look at the asynchronous web ways of the server side to provide similar functions. Asynchronous Web methods have high performance similar to HSAPI extensions, but do not need to write code to manage your own thread pool, and all the advantages running in managed code.
First we consider a regular synchronous Microsoft® ASP.NET Web method. When you return from the synchronization web method, the response to this method will be sent. If a longer time is required to complete the request, the process of processing the request will always occupy until the method call ends. Unfortunately, most longer calls are caused by a longer database query or an event such as a call to another Web service. For example, if you call the database, the current thread will wait for the call to complete. Threads can be done, just waiting until the return of the query is heard. Similar problems will occur when the thread waits to complete the call to the TCP socket or the backend web service.
Let the thread are in a waiting state, especially if the server's operating pressure is large. Waiting for threads do not work any effective job, such as providing services for other requests. We need to find a way to start longer background processes on the server, while returning the current thread to the ASP.NET process pool. Then, when a longer backend process is completed, we call a callback function to end the process of the request, and notify the ASP.NET request in some way. In fact, this feature can be provided by ASP.NET to use asynchronous web methods.
Working principle of asynchronous web methods
When you write a typical ASP.NET Web service using the web method, Microsoft® Visual Studio® .NET is just compiling your code to create an assembly; the assembly will be called when receiving a request for its web method. The assembly itself does not know anything about SOAP. Therefore, when your application starts for the first time, the ASMX handler must reflect your assembly to determine which Web methods are provided. For conventional synchronous requests, these operations are simple: find which methods have associated webMethod properties, based on the SOAPAction HTTP header to set the logic of calling the correct way.
For asynchronous requests, during the reflection, the ASMX handler finds a Web method with some signature and identifies the signature as an asynchronous. This handler will find ways to meet the following rules:
Beginxxx and Endxxx Web methods, where xxx is any string, indicating the name of the method to be provided.
The Beginxxx function returns an IASYNCRESULT interface and accepts asyncCallback and an object, as its last two input parameters.
The ENDXXX function accepts an IASYNCRESULT interface as its unique parameter.
Both methods must be identified using the WebMethod property.
If the ASMX handler finds that two methods meet all of the above conditions, the method XXX is provided as a conventional web method in its WSDL. This method will accept the parameters defined before the AsyncCallback parameter in Beginxxx's signature as input and return the content returned by the ENDXXX function. Therefore, if a Web method has the following synchronization declaration: [WebMethod]
Public String lengthProcedure (int MilliseConds) {...}
The asynchronous declaration will be:
[WebMethod]
Public IASYNCRESULT BeginLengthyProcedure
Int milliseconds,
AsyncCallback CB,
Object s) {...}
[WebMethod]
Public String end (IasyncResult Call) {...}
The WSDL of each method is the same.
After the ASMX handler reflects the assembly and detects an asynchronous web method, it must handle requests to the method in a way different from the processing synchronization request. It will call the Beginxxx method, not a simple method. It sequences the incoming request restore to the parameters to be passed to the function (like the processing synchronous request); however, it also passes the pointer to an internal callback function (as an additional ASYNCALLBACK parameter for the Beginxxx method).
This approach is similar to the asynchronous programming mode of the Web service client application in the .NET Framework. If the client supports the asynchronous web service call, you can release the usage thread for the client computer; if the server supports the asynchronous web service call, you can release the threads occupied by the server computer. But there are two key differences here. First, it is not called the server code to call the Beginxxx and Endxxx functions, but is called by the ASMX handler. Second, you have to write code for the Beginxxx and Endxxx function, and cannot use the code generated by the Add Web Reference wizard by WSDL.exe or Visual Studio .NET. However, the result is the same, that is, release the thread to enable it to perform other processes.
After the ASMX handler calls the server's Beginxxx function, the thread returns the thread to the process thread pool to process any other requests received. However, the requestible HTTPCONTEXT cannot be released. The ASMX handler will wait until it passes the callback function of the Beginxxx function being called, it ends the processing request.
Once the callback function is called, the ASMX handler will call the ENDXXX function to make your web method to complete any of the processed to be executed, and can be serially serized to the return data in the SOAP response. The ENDXXX function returns a response, only the requested HTTPCONText is released.