Simple asynchronous Web method

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.