Friday, 5 June 2015

Message Exchange pattern in WCF - Duplex Pattern

This post is continuation of post "Messasge Exchange pattern in WCF". Please read following post before proceeding this post.
http://logicsmaze.blogspot.co.uk/2015/06/message-exchange-pattern-in-wcf.html 

Duplex message Exchange Pattern :

When client send request to service and request is time consuming like fetching report.While service is executing the request client have to wait for completion of request.But with duplex message exchange pattern, within the execution period of request service can also send messages to client back, like progress percentage of request or which steps is currently executing etc. and client can utilize this data within waiting period.

Duplex message exchange pattern is not supported by basicHttpBinding and wsHttpBinding as well so implementing this you have to use netTcpBinding.

Note : Visit following like to know hou to use netTcpbinding in wcf.

Steps to Implement Duplex message exchanging pattern.

Step 1 : Add new WCF service.      

   [ServiceContract(CallbackContract = typeof(IReportProgressCallback))]  
    public interface IReportService
    {    

        [OperationContract]
        void ProcessReport();      
     
    }
    [ServiceContract]
    public interface IReportProgressCallback
    {
        [OperationContract]
        void ReportProgress(int percentage);
    }

In above code a new service contract for callback should be declared.To link this callback contract with IReportService contract you have to use Callbackcontract attribute of [ServiceContract] (refer highlighted line in above code)

Step 2 : Implement these Interfaces :

[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant)]
public class Service1 : IReportService
    {
 public void ProcessReport()
        {
            for (int i = 1; i < 100; i++)
            {
                Thread.Sleep(100);
                OperationContext.Current.GetCallbackChannel<IReportProgressCallback>().ReportProgress(i);
            }
        }
    }

Note : If you don't use green highlighted lines of code, while using service you will get following error.


Step 3 : Create new client Windows form project.

 [CallbackBehavior(UseSynchronizationContext=false)]
    public partial class Form1 : Form, ReportService.IReportServiceCallback
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Process_Click(object sender, EventArgs e)
        {
            InstanceContext instanceContext=new InstanceContext(this);
            ReportService.Service1Client client = new ReportService.Service1Client(instanceContext);
            client.ProcessReport();
        }

        public void ReportProgress(int percentage)
        {
            TextBox.CheckForIllegalCrossThreadCalls = false;
            textBox1.Text = percentage.ToString() + "% completed";
        }
     
    }

When you run windows application client, output will be like follows:
On clicking Process button, client can display callback messages on text-box and label to show progress report.


















If you want to implement same with One-Way pattern than remove following highlighted lines from existing code and add IsOneWay attribute of [OperationContract] true.

[CallbackBehavior(UseSynchronizationContext=false)]
    public partial class Form1 : Form, ReportService.IService1Callback
{

and


[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant)]
public class Service1 : IReportService
    {


And use

 [OperationContract(IsOneWay = true)]
        void ProcessReport();

 [OperationContract(IsOneWay=true)]
        void ReportProgress(int percentage);


Now run client with One-Way message exchange pattern.

Related Topics : 
Message-exchange-pattern-in-wcf
Different-option-of-hosting-wcf-service
Binding-in-wcf-choosing-right-wcf-service
Hosting WCF with Non-Http protocol
Exchanging-metadata-in-wcf
Some-interesting-facts-about-data-contract
Knowntype-attribute-in-wcf
Associating-knowntype-in-wcf
Message-contract-in-wcf
Exception-handling-in-wcf
Exception Handling in WCF - SOAP Fault in WCF
Exception Handling in WCF - Unhandled Exception in WCF
Exception Handling in WCF - Creating and Throwing Strongly Typed SOAP Fault
Instance-context-mode-in-wcf



No comments:

Post a Comment