In: Computer Science
How can we transform a "synchronous" web method to "asynchronous" web methods?
Synchronous versus asynchronous
In Synchronous call, on the off chance that you are making any solicitation, at that point you should hold up till the reaction, you can't do some other thing until you won't get the reaction.
In Asynchronous call, in the event that you are making any solicitation, at that point you don't have to hang tight for the reaction and you can play out some other errand. At whatever point the reaction will come, you can get in get back to designate.
So on the off chance that any solicitation won't require some investment, at that point you can utilize synchronous call else you can utilize nonconcurrent. Or on the other hand on the off chance that you are calling web administration technique in circle (like you need to accomplish something in framework cell), at that point you should utilize synchronous way.
We transform synchrnoys web methods to asynchronous methods as follows :-
There is a BeginXXX and EndXXX web method where XXX is any
string that represents the name of the method you want to
expose.
The BeginXXX function returns an IAsyncResult interface and takes
an AsyncCallback and an object as its last two input
parameters,respectively.
The EndXXX function takes an IAsyncResult interface as its only
parameter.
Both the BeginXXX and EndXXX methods must be flagged with the Web
Method attribute.
Synchronous web method
[WebMethod]
public string Sleep(int milliseconds)
{
Thread.Sleep(milliseconds);
}
Example.Asynchronous web methods
[WebMethod]
public IAsyncResult BeginSleep(
int milliseconds,
AsyncCallback cb,
object s) {...}
[WebMethod]
public string EndSleep(IAsyncResult call) {...}