Server-side asynchronous Web methods

Source: Internet
Author: User
Tags call back microsoft sql server socket thread web services advantage
web| Server | Asynchronous Web method when asynchronous

There are a few issues to consider when determining whether it is appropriate to adopt an asynchronous Web method in your application. First, the called BeginXXX function must return a IAsyncResult interface. IAsyncResult are returned from multiple asynchronous I/O operations, including accessing the data stream, making microsoft®windows® socket calls, performing file I/O, interacting with other hardware devices, invoking asynchronous methods, and of course invoking other WEB services. You can get IAsyncResult from these asynchronous operations to return it from the BeginXXX function. You can also create your own classes to implement the IAsyncResult interface, but you may then need to wrap some of the previously mentioned I/O operations in some way.

For most asynchronous operations mentioned earlier, it makes sense to use asynchronous Web methods to wrap back-end asynchronous calls, making Web service code more efficient. Except when using a delegate to make an asynchronous method call. Delegates cause asynchronous method calls to occupy a thread in the process thread pool. Unfortunately, the ASMX handler also uses these threads when servicing incoming requests. So unlike calls to perform real I/O operations on hardware or network resources, asynchronous method calls that use delegates will still occupy one of the process threads while executing. You can also use the original thread to run your Web methods synchronously.

The following example shows an asynchronous Web method that invokes a back-end Web service. It has identified the Begingetage and Endgetage methods using the WebMethod property to run asynchronously. The code for this asynchronous Web method calls the back-end Web method named Userinfoquery to get the information it needs to return. Calls to the Userinfoquery are executed asynchronously and are passed to the AsyncCallback function, which is passed to the Begingetage method. This will cause the internal callback function to be invoked when the backend request completes. 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 back-end processing in the same thread pool that serves the middle-tier Web method request.

[WebService]

public class GetMyInfo:System.Web.Services.WebService

{

[WebMethod]

Public IAsyncResult Begingetage (AsyncCallback cb, Object State)

{

Invokes an asynchronous Web service call.

localhost. Userinfoquery Proxy

= new localhost. Userinfoquery ();

Return proxy. Begingetuserinfo ("username",

Cb

proxy);

}



[WebMethod]

public int Endgetage (IAsyncResult res)

{

localhost. Userinfoquery Proxy

= (localhost. Userinfoquery) Res. asyncstate;

int age = Proxy. Endgetuserinfo (RES). Age;

The results of this WEB service are otherwise

Processing.

return age;

}

}

One of the most common types of I/O operations that occurs in a Web method is a call to an SQL database. Unfortunately, Microsoft®ado.net has not yet defined a good asynchronous invocation mechanism, and simply wrapping the SQL calls into asynchronous delegate invocations has little to do with improving efficiency. Although you can sometimes choose to cache results, you should also consider using Microsoft SQL Server Web Services Toolkit to publish your database as a Web service. This enables you to invoke the Web service asynchronously to query or update the database, using the support in the. NET Framework.

When accessing SQL through a WEB service invocation, you need to be aware of many back-end resources. If you use a TCP socket to communicate with a Unix computer, or access a few other available SQL platforms through a dedicated database driver, or even a resource that is accessed using DCOM, you can consider using a number of Web services toolkits to publish these resources as Web services.

One of the advantages of using this approach is that you can take advantage of the client Web service architecture, such as asynchronous Web service calls that use the. NET Framework. This allows you to get asynchronous invocation capabilities for free, and your client access mechanism works efficiently with asynchronous Web methods.

Aggregating data using asynchronous Web methods

Many Web services now access multiple resources at the back end and aggregate information for the front-end Web services. Although invoking multiple back-end resources increases the complexity of the asynchronous Web method model, it can ultimately improve efficiency significantly.

Suppose your Web method calls two back-end Web services: Service A and service B. From your BeginXXX function, you can invoke service A and service B asynchronously. You should pass your own callback function to each asynchronous call. After receiving the results from service A and service B, the callback function that you provide to trigger the completion of the Web method verifies that all requests have been completed, that all the processing is done on the returned data, and then calls the callback function passed to the BeginXXX function. This will trigger a call to the ENDXXX function, which will result in the completion of the asynchronous Web method.

Summary

Asynchronous Web methods provide an efficient mechanism in asp.net Web services to call back-end services without taking advantage of valuable threads in the process thread pool. By combining asynchronous requests for back-end resources, the server can use its own WEB methods to maximize the number of simultaneous requests processed. You should consider using this method to develop High-performance Web service applications.



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.