[Technical Review Series]-asynchronous WebService call

Source: Internet
Author: User

In general, the call to WEB methods in WEB services is always waiting until the client sends a request until the returned result is returned, in this way, if the processing of some WEB methods takes a long time to complete, the program efficiency will be greatly reduced. However, the asynchronous call method can effectively solve this problem, so that the client can do other things within the waiting time after the request is sent, rather than staying there and waiting.
Understanding of asynchronous calls:
To put it bluntly, asynchronous calls are actually executed in parallel by the caller thread and the thread that executes the called process.
There are four methods to Implement Asynchronous invocation of WEB service methods.
Use callback
To use this method for asynchronous calls to WEB Services, the key is to pass in a proxy instance as the callback method at the end of the call when an asynchronous call is started. In this way, the method used for callback calls ends the asynchronous call to obtain the result of the asynchronous call. If the caller needs to synchronize with an asynchronous call, you need to input a synchronization object [as the last parameter] when starting the asynchronous call. then, the object is obtained through the AsyncState member of IAsyncResult In the callback method.
For example:
Assume that the following WEB service method is available:
[Webmethod]
Public string GetName (string name)
{Return name ;}
******************** ******
Class Client
{
Localhost1.Serivice service = new localhost1.Service ();
Public void GetResults (IAsyncResult ar)
{
MannualResetEvent ent;
Ent = (MannualResetEvent) ar. AsyncState;
String ret = service. EndGetName (ar );
Console. WriteLine ("the result is:" + ret );
Ent. Set ();
}

Public void CallWithBack ()
{
String name = "kim ";
AsyncCallBack callBack;
CallBack = new AsyncCallBack (GetResults );
MannualResetEvent ent = MannualResetEvent (false );
Service. BeginGetName (name, callBack, ent );
// Do other things
Ent. WaitOne ();
}
Static void Main (string [] ars)
{
CallWithBack ();
}
}
Method 2 use round robin
Round Robin checks the IsCompleted attribute of the IasyncResult variable obtained after the asynchronous call is started to wait until the asynchronous call ends. Generally, when judging that the asynchronous call is not over, calling the static method Sleep (0) of the Thread class forces the current Thread to be transferred from the running state to the ready state.
For example:
The WEB service method is as follows:
Client implementation:
Class Client
{
Localhost1.Serivice service = new localhost1.Service ();
Public void CallWithQuery ()
{
String name = "kim ";
IasyncResult ar;
Ar = service. BeginGetName (name, null, null );
While (! Ar. IsCompleted)
{
// Do other things
Console. WritleLine ("Watting ....");
Tread. Sleep (0 );
}
String ret;
Ret = service. EndGetName (ar );
Console. WriteLine ("the result is:" + ret );
}
Static void Main (string [] ars)
{
CallWithQuery ();
}
}
The third method is to start and end the call.
This method gets the result after an asynchronous call is started. If the asynchronous call is not completed, the current line is blocked.
For example:
The WEB service method is as follows:
Client implementation:
Class Client
{
Localhost1.Serivice service = new localhost1.Service ();
Public void CallWithEnd ()
{
String name = "kim ";
IasyncResult ar = service. BeginGetName (name, null, null );
String ret = service. EndGetName (ar );
Console. WriteLine ("the result is:" + ret );
}
Static void Main (string [] ars)
{
CallWithEnd ();
}
}
The last method is: start the call, wait for processing, and end the call.
After the asynchronous call is started, the customer receives the returned IasyncResult Member, calls the line to wait on the synchronization object indicated by the AsyncWaitHandle attribute of IasyncResult, and ends the asynchronous call to obtain the result.
For example:
The WEB service method is as follows:
Client implementation:
Class Client
{
Localhost1.Serivice service = new localhost1.Service ();
Public void CallWithWaitEnd ()
{
String name = "kim ";
IasyncResult ar = service. BeginGetName (name, null, null );
String ret;
// Do other things
If (ar. AsyncWaitHandle. WaitOne (1000, false ))
{
Console. WriteLine ("Invoke over ");
Ret = service. EndGetName (ar );
}
Else
{
Console. WriteLine ("Continue watting ...");
Ret = service. EndGetName (ar );
Console. WriteLine ("Invoke over ");
}
Console. WriteLine ("the result is:" + ret );
}
Static void Main (string [] ars)
{
CallWithWaitEnd ();
}
}

Ps: In the generated proxy class, the system has automatically created an asynchronous call interface for us, so we do not need to implement it again. Such as Begin *** and End. The asynchronous interface of the preceding WEB method GetName in the proxy class should be as follows:
Public System. IasyncResult BeginGetName (string name, System. AsycnCallback callback, object asyncState)
Public string EndGetName (System. IasyncResult reult)
For other functions, refer to MSDN.

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.