Saturday 28 January 2012

How to Create Callback Service in WCF

This tutorial gives hands-on to create a sample Callback service.
Step 1: Create the sample Classlibrary project using Visual Studio 2008 and name it as CallbackService
 
 
 
Step 2 : Add System.ServiceModel reference to the project
Step 3: Create the Callback and Service contract as shown below. You need to mention CallbackContract property in the ServiceContract attribute. Implementation of the Callback contract will be done on the client side.
IMyContract.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace CallbackService
{
    
    public interface IMyContractCallback
    {
        [OperationContract]
        void OnCallback();
    }
    [ServiceContract(CallbackContract = typeof(IMyContractCallback))]
    public interface IMyContract
    {
        [OperationContract()]
        void MyMethod();
    }

}
Step 4: Implement the Service contract as shown below. In the below code you will find using OperationContext is used to receive the reference to Callback instance. Using that instance we are calling the OnCallback() method from client side.
MyService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace CallbackService
{
    [ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple )]
   public class MyService:IMyContract 
    {
        public void MyMethod()
        { 
            //Do something            
            IMyContractCallback callbackInstance
            =OperationContext.Current.GetCallbackChannel();
            callbackInstance.OnCallback();
        }
    }
}
You can also note that We have set the ConcurrencyMode to Multile. If you are not using ConcurrencyMode to Multiple or Reentent, you will be end up with deadlock exception as shown below. This is because when a client made a call to the service, channel is created and lock by WCF service. If you are calling the Callback method inside the service method. Service will try to access the lock channel, this may leads to deadlock. So you can set ConcurrencyMode to Multiple or Reentent so it will release the lock silently.
Step 5: Create a Console application using Visual Studio 2008 and name it a CallbackServiceHost. This application is used to self-host the WCF service
Step 6: Main method
        static void Main(string[] args)
        {
            Uri httpUrl = new Uri("http://localhost:8090/MyService/");
             ServiceHost host = new ServiceHost(typeof(CallbackService.MyService), httpUrl);
            host.Open();
            Console.WriteLine("Service is Hosted at {0}", DateTime.Now.ToString());
            Console.WriteLine("Host is running...Press  key to stop the service.");
            Console.ReadLine();
            host.Close();
        }
  
Step 7: Use Duplex binding to support Callback operation.
Web.Config
<system.serviceModel>
    <services >
      <service behaviorConfiguration="ServiceBehavior"  
      name="CallbackService.MyService">
        <endpoint address="http://localhost:8090/MyService" 
        binding="wsDualHttpBinding" contract="CallbackService.IMyContract">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" 
        binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true "/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
Step 8: Run the host application
Step 9: Create Console Application using Visual Studio 2008 and name it as CallbackClient. This is the client application which contain Callback implementation.
Step10: Add System.ServiceModel and CallbackService as reference to the project
Step 11: Create the proxy class as shown below. Use DuplexClientBase to create the proxy, because it will support bidirectional communication. Create the contractor which will accept InstanceContext as parameter.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using CallbackService;

namespace CallbackClient
{
    class MyServiceClient:DuplexClientBase<IMyContract>,IMyContract
    {
        public MyServiceClient(InstanceContext callbackCntx)
            : base(callbackCntx)
        {            
        }
        public void MyMethod()
        {
             base.Channel.MyMethod();
        }
    }
   
}
Step12: Create the implementation for Callback Contract
class MyCallback : IMyContractCallback
    {
        public void OnCallback()
        {
            Console.WriteLine("Callback method is called from client side.");
            
        }
    }
Step 13: Implementation of main method
  static void Main(string[] args)
        {
            IMyContractCallback callback=new MyCallback();
            InstanceContext cntx=new InstanceContext(callback);

            MyServiceClient proxy = new MyServiceClient(cntx);
            Console.WriteLine("Client call the MyMethod Operation from Service.");
            proxy.MyMethod();            
            Console.ReadLine();
        } 
Step14: Run the client application. In the output, you can see the OnCallback method called by the service

No comments:

Post a Comment