When to use the asynchronous Web method
When determining whether the asynchronous Web method is suitable for your application, consider several issues. First, the BeginXXX function must return an IAsyncResult interface. IAsyncResult is returned from multiple asynchronous I/O operations, including accessing data streams and performing Microsoft & reg; Windows & reg; SOCKET call, execution file I/O, interaction with other hardware devices, and Asynchronous Method call, including calling other Web Services. You can obtain IAsyncResult from these asynchronous operations to return it from the BeginXXX function. You can also create your own class to implement the IAsyncResult interface, but you may need to encapsulate an I/O operation mentioned above in some way.
For most asynchronous operations mentioned above, it is meaningful to use the asynchronous Web method to encapsulate backend asynchronous calls, which can make Web Service Code more effective. However, this does not apply to asynchronous method calls using delegation. Delegation will cause Asynchronous Method calls to occupy a thread in the thread pool of the process. Unfortunately, the ASMX handler also uses these threads when providing services for incoming requests. Therefore, unlike calling for real I/O operations on hardware or network resources, calling using a delegated Asynchronous Method still occupies one of the Process threads during execution. You can also use the original thread to run your Web method synchronously.
The following example shows an asynchronous Web method that calls the backend Web service. It has used the WebMethod attribute to identify the BeginGetAge and EndGetAge methods for asynchronous operation. The code of this asynchronous Web method calls the backend Web method named UserInfoQuery to obtain the information it needs to return. The call to UserInfoQuery is asynchronously executed and passed to the AsyncCallback function. The latter is passed to the BeginGetAge method. This will call the internal callback function when the backend request is complete. The callback function then calls the EndGetAge method to complete the request. The code in this example is much simpler than the code in the previous example and has another advantage, that is, it does not start backend processing in the same thread pool as providing services for the middle-layer Web method requests.
[WebService]
Public class GetMyInfo: System. Web. Services. WebService
{
[WebMethod]
Public IAsyncResult BeginGetAge (AsyncCallback cb, Object state)