Saturday, August 15, 2015

WCF Duplex(Two way) contracts

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-
  1. Create new callback interface, with one method that will execute on completion of main method
    public interface IClientCallBack
    {
        [OperationContract(IsOneWay=true)]
        void OperationCompleted(string message);
    }
  2. 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);
    }

  3. Mark both the methods IsOneWay=true, main service method & completion method
  4. Call completion method as last statement of main service method
    public 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");
       }         
    }
  5. 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-
  1. 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 method
    public class ClientCallBack : SerDuplex.IService1Callback
    {
        public void OperationCompleted(string message)
        {
          Console.WriteLine("Service method execution completed..." + message  + " "+             DateTime.Now.ToString());
          Console.ReadLine();
        }
    }
  2. Create object of InstanceContext class with passing object of new created class
    InstanceContext IC = new InstanceContext(new ClientCallBack());
  3. 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

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...