Asynchronous call of Web Services

Source: Internet
Author: User
Tags net thread

The portal built based on AJAX technology is the most successful web application in the Web 2.0 generation.Program. In this market, igoogle and pageflakes are already at the forefront of the times.

When you open pageflakes, you will see the following interface:

Next, each "part" on the Interface requests various web services from the server, and the server acts as a proxy to send requests to external web services on behalf of the server. (This is because Ajax calls cannot be crossed, so data is often requested through a proxy)

Problem scenarios: A very popular "part" cannot be executed for a long time. As a result, the request cannot be executed in a timely manner, causing a request failure (timeout), or even a large amount of access may cause the server to crash, the user cannot access the web site.

Solution: As soon as users enter the page they access, each "part" sends a call request through the proxy Web Service, which retrieves data from the external service. The external service either runs quickly or times out. Therefore, we must set a long time-out period because services with a large volume of external data are generally executed slowly. In the long run, Asp.net Ajax does not support asynchronous web method calls. To solve this problem, we must change the proxy web service to an asynchronous HTTP processor. This is what we will introduce below --AsynchronousCall web service methods.

AsynchronousCall web service methods

By default, all web methods stated on the server web service are executed synchronously. However, the calls sent from the browser through xml http objects are asynchronous, but they are synchronized when the server executes the web method. This means that a response to a web method call will be generated from the moment the request arrives, and it will occupy a thread from the Asp.net workthread pool. If it takes a relatively long time from the request to the completion of the call, the request thread will continue to process until the call method ends. Unfortunately, most of the calls that require a long period of time are long database queries or calls to another web service. For example, if you need to call the database, the current thread will wait until the call is completed. The thread does nothing until it detects the returned query results. When a thread is waiting
A similar problem occurs when a socket or a Web service in the background is called.

When you construct a typical Asp.net web service application by using the web method, the compilerCodeCompile as an assembly so that the web method of the request is called when it is received. When your application is started for the first time, the asmx processor determines which web method to expose by reflecting the assembly.

For normal synchronous requests, the simple way is to find out which methods are associated with the [webmethod] features.

To make the web method asynchronous, make sure that the following rules are met:

There is a beginxxx and endxxx web method, where XXX is an arbitrary string, it represents the name of the method you want to expose.

1. The beginxxx method returns an iasyncresult interface and uses asynccallback and an object as the last two input parameters.

2. The endxxx method has an iasyncresult interface as its unique parameter.

3. The beginxxx and endxxx methods must be marked as webmethod features.

If the asmx processor finds a method that meets all of the preceding conditions, it will then be exposed to the WSDL document like a normal web method.

The following shows the definition of a synchronization method:

[Webmethod] <br/> Public String sleep (INT miilliseconds) <br/>{< br/> thread. Sleep (miilliseconds); <br/>}< br/>

The following is the definition of asynchronous web method:

[Webmethod] <br/> Public iasyncresult sleep (INT miilliseconds, asynccallback CB, object s ){......} </P> <p> [webmethod] <br/> Public String endsleep (iasyncresult call ){......} <br/>

The asmx processor exposes a web method named sleep from this method. This method will accept the type of the parameter that was previously returned in the beginxxx method signature using the asynccallback type as the input parameter and endxxx method.

After the asmx processor performs reflection processing on the compiled assembly and detects an asynchronous web method, it must process the requests (beginxxx, endxxx) of these different Methods Compared with synchronous requests ). Instead of synchronously calling the sleep method and generating a response from the returned value, the beginsleep method is called. It deserializes the sent request into parameters passed to the function. This process is usually for Synchronous requests, however, it also transmits a pointer to the internal callback function like passing an extra asynchronous callback parameter to the beginsleep method. 【This is a bit like calling an instance method. If a class has two objects, A and B, and the class has an instance method run (), you may ask how the compiler distinguishes. run () and B. run ()? In fact, the compiler will pass the caller of the current method as a parameter to the call method.].

When the asmx processor calls the beginsleep method, it will return the thread to the thread pool of the Asp.net process, so it can process other requests. However, the httpcontext of the request will not be released. Httpcontext will wait until the call to the beginsleep callback function that completes the request processing is completed.

Once the callback function is called, a thread is taken from the thread pool to execute the remaining work. To complete multiple processing requests to be executed and present data as a response, the asmx processor calls the endsleep method. Once the response is sent, httpcontext is released.

Of course there are still some basic principles and limitations to consider:

(1)When you use the business logic layer to read and write data that is not asynchronously processed, you cannot use the asynchronous web method.

(2)When you call external web services synchronously, you cannot use Asynchronous methods. External calls must also be asynchronous.

(3)When you use the conventional synchronous method to perform database operations, you cannot use the Asynchronous Method. Only all database operations must be asynchronous.

(4)When I/O operations that do not need to be waited, such as HTTP requests, Web service calls, remoting, asynchronous database calls, or asynchronous file operations, this is of no benefit to asynchronous web method calls. You will not benefit from a simple delegate. begininvoke call to execute asynchronous methods, because asynchronous Methods assign threads from the same thread pool, such as the thread pool in Asp.net.

The above warning can be summarized as two points:

(1)If an asynchronous operation requires multiple asynchronous calls, each step must be asynchronous, that is, all the way to the end ].-- Reference from blog

(2)I/O-intensive operations consider Asynchronization, and computing-intensive operations consider multithreading (I/O operations without waiting are not worth Asynchronization ).

Therefore, in the above sample code, simple sleep methods and any methods used to proxy web services cannot be truly asynchronous methods. We need to rewrite them to support asynchronous calls. Before we start, remember a principle. When the beginxxx web method calls the beginyyy method of some other components and your endxxx method calls the endyyy method of this component, you can only benefit from Asynchronous methods. Otherwise, asynchronous use of the web method has no benefits.

The following shows a simple code for implementing the stock quote proxy web service. The begingetstock method of the proxy web service calls the begingetstock method of another component. This method captures data from external sources. After data is obtained, the component performs a callback through the asynccallback CB object. The asmx processor program passes the callback down to the web method. Therefore, when it is called, Asp.net's asmx handler will receive the callback and restore the httpcontext object, call the endgetstock method and present the response.

[WebService] <br/> public class stockquoteproxy: system. web. services. webService <br/>{< br/> [webmethod] <br/> Public iasyncresult begingetstock (iasynccallback CB, object state) <br/>{< br/> net. stockquote. stockquoteservice porxy = new net. stockquote. stockquoteservice (); <br/> return porxy. begingetstock ("MSFT", CB, proxy); <br/>}</P> <p> [webmethod] <br/> Public String endstock (iasyncresult res) <br/>{< br/> <span style = "white-space: pre"> </span> net. stockquote. stockquoteservice porxy = (net. stockquote. stockquoteservice) res. asyncstates; <br/> <span style = "white-space: pre"> </span> string quotes = proxy. endgetstock (RES); <br/> <span style = "white-space: pre"> </span> return quotes; <br/>}< br/>

Summary

As shown above, the asmx handler of Asp.net provides the ability to call asynchronous web methods and return threads to the Asp.net thread pool, while ASP. the asmx processing program of the netajax framework does not have such capabilities. It only supports synchronous calls. Therefore, we need to rewrite the asmx processing program of the Asp.net Ajax framework to support asynchronous web method execution, and bypass the asmx processing program of ASP. netajax when the web method is called through xml http. Next, I want to explain how the asmx OF THE Asp.net Ajax framework works, and how to rewrite your own processing program that introduces many new features.


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.