The asynchronous call of WCF is implemented based on message exchange, which is different from the asynchronous call by Delegate.
Procedure:
1. Create a service contract.
[Servicecontract (sessionmode = sessionmode. Required)]
Public interface icalculate
{
[Operationcontract]
Int add (int A, int B );
}
2. Add an asynchronous version for the contract method.
We have added the inadd and endadd Asynchronous Operation Methods "common" in. Net SDK. Note that the asynchronous declaration is added to the beginadd method, but the endadd method does not. Pay attention to the parameters of the asynchronous version method.
[Servicecontract (sessionmode = sessionmode. Required)]
Public interface icalculate
{
[Operationcontract]
Int add (int A, int B );
[Operationcontract (asyncpattern = true)]
Iasyncresult beginadd (int A, int B, asynccallback callback, object State );
Int endadd (iasyncresult AR );
}
3. Implement the service contract.
You may have noticed that we have not created the principal prototype of ADD, nor "real" Implementation of beginadd and endadd. This is because message exchange calls the add method asynchronously. All asynchronous version methods are only used to create message declarations.
Public class calculateservice: icalculate
{
Public int add (int A, int B)
{
Thread. Sleep (5000 );
Console. writeline (operationcontext. Current. sessionid );
Return A + B;
}
Public iasyncresult beginadd (int A, int B, asynccallback callback, object state)
{
Throw new exception ("the method or operation is not implemented .");
}
Public int endadd (iasyncresult AR)
{
Throw new exception ("the method or operation is not implemented .");
}
}
4. Let's give a complete version to see the execution result.
[Servicecontract]
Public interface icalculate
{
[Operationcontract]
Int add (int A, int B );
[Operationcontract (asyncpattern = true)]
Iasyncresult beginadd (int A, int B, asynccallback callback, object State );
Int endadd (iasyncresult AR );
}
Public class calculateservice: icalculate
{
Public int add (int A, int B)
{
Console. writeline ("server Method Add: {0}", datetime. Now );
Try
{
Thread. Sleep (5000 );
Return A + B;
}
Finally
{
Console. writeline ("server Method Add execution completed: {0}", datetime. Now );
}
}
Public iasyncresult beginadd (int A, int B, asynccallback callback, object state)
{
Throw new exception ("the method or operation is not implemented .");
}
Public int endadd (iasyncresult AR)
{
Throw new exception ("the method or operation is not implemented .");
}
}
Public class wcftest
{
Public static void test ()
{
Appdomain. createdomain ("server"). docallback (delegate
{
Servicehost host = new servicehost (typeof (calculateservice ));
Host. addserviceendpoint (typeof (icalculate), new wshttpbinding (),
"Http: // localhost: 8080/calc ");
Host. open ();
});
Icalculate channel = channelfactory <icalculate>. createchannel (New wshttpbinding (),
New endpointaddress ("http: // localhost: 8080/calc "));
Using (channel as idisposable)
{
Console. writeline ("the client calls beginadd: {0}", datetime. Now );
Iasyncresult AR = channel. beginadd (1, 2, delegate {console. writeline ("Callback...") ;}, null );
Console. writeline ("the client calls beginadd to complete: {0}", datetime. Now );
Console. writeline (Channel. endadd (AR ));
Console. writeline ("client calls endadd to complete: {0}", datetime. Now );
}
}
}
Output:
Client call beginadd: 2007-4-1 20:56:47
Client call beginadd completed: 20:56:47
Run add on the server method: 20:56:49
Server Method Add execution completed: 20:56:54
3
Client call endadd completed: 20:56:55
Callback...
Finally, we must use the binding object that supports session (basichttpbinding throws an exception ).
Note: The simplest way is not to manually add asyncpattern/beginxxx/endxxx, but to manually use svcutil.exe to generate a client proxy. Remember to add the "/async" parameter.