Duplex contract is two way contract, two channels establishes between service & client, client to service & service to client.
Requirement: Whenever we have big service operation that we want to execute one way (Asynchronously), but we want to get notified also when service operation get completed.
Benefit: This way without getting halted for service method completion client get notified when operation completed also can get some return value from service.
Supported bindings : wsDualHttpBinding, netNamedPiepeBinding, netTCPBinding
Only these bindings are able to establish two transport channels.
Steps to implement Two way binding:
Service Side-
Results:
Requirement: Whenever we have big service operation that we want to execute one way (Asynchronously), but we want to get notified also when service operation get completed.
Benefit: This way without getting halted for service method completion client get notified when operation completed also can get some return value from service.
Supported bindings : wsDualHttpBinding, netNamedPiepeBinding, netTCPBinding
Only these bindings are able to establish two transport channels.
Steps to implement Two way binding:
Service Side-
- Create new callback interface, with one method that will execute on completion of main methodpublic interface IClientCallBack{[OperationContract(IsOneWay=true)]void OperationCompleted(string message);}
- Assign type of new created callback interface to attribute CallbackContract in ServiceContract of main interface[ServiceContract(CallbackContract= typeof(IClientCallBack))]public interface IService1{[OperationContract(IsOneWay=true)]void SetData(int value);
- Mark both the methods IsOneWay=true, main service method & completion method
- Call completion method as last statement of main service methodpublic class Service1 : IService1{public void SetData(int value){Thread.Sleep(value);//should be last statement OperationContext.Current.GetCallbackChannel<IClientCallBack().OperationCompleted("Done " + value/1000 + " seconds taken");}}
- Implement wsDualHttpBinding in webconfig<behaviors><serviceBehaviors><behavior name="serviceBehavior" ><serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/><serviceDebug includeExceptionDetailInFaults="false"/></behavior></serviceBehaviors></behaviors><services><service name="WCFDuplexContract.Service1" behaviorConfiguration="serviceBehavior" ><endpoint address="" binding="wsDualHttpBinding" contract="WCFDuplexContract.IService1" bindingConfiguration="dualBinding" ></endpoint></service></services><bindings><wsDualHttpBinding><binding name ="dualBinding"></binding></wsDualHttpBinding></bindings>
Client Side-
- Create new class & implement new created callback interface at client side, define completion method in class, you can get return message(any other WCF supported data types) as parameter of this methodpublic class ClientCallBack : SerDuplex.IService1Callback{public void OperationCompleted(string message){Console.WriteLine("Service method execution completed..." + message + " "+ DateTime.Now.ToString());Console.ReadLine();}}
- Create object of InstanceContext class with passing object of new created classInstanceContext IC = new InstanceContext(new ClientCallBack());
- Pass this object of InstanceContext class to service while creating service object
SerDuplex.Service1Client _client = new SerDuplex.Service1Client(IC);
static void Main(string[] args)
{
InstanceContext IC = new InstanceContext(new ClientCallBack());
SerDuplex.Service1Client _client
= new
SerDuplex.Service1Client(IC);
Console.WriteLine("Client
execution started..." + DateTime.Now.ToString());
_client.SetData(10000);
Console.WriteLine("Client
execution completed..." + DateTime.Now.ToString());
Console.ReadLine();
}
Results:

No comments:
Post a Comment