Message Exchange Patterns In WCF

A message exchange pattern means how the client and WCF service exchange the message (data) internally. There are a total three types of patterns,

  1. Request – Reply
  2. One Way
  3. Duplex

We will learn each of them in detail below.

  1. Request – Reply

    In request reply pattern client sends the request to WCF service and waits for the response from the WCF service. Until they get a reply no other process runs. After getting a response from the WCF service they will continue its work. If WCF service has a method with void return type then the client will wait for the reply.

    So in this pattern if any error occurs then it will directly display on the client side. Default pattern for any methods is request – reply. You can declare any method to request a reply by assigning ‘IsOneWay=false’ as show below.
    1. [ServiceContract]  
    2.         public interface IMessageExchPatterns  
    3.         {  
    4.             [OperationContract]  
    5.             string GetDataRequestReply (string name);  
    6. }
    OR
    1. [ServiceContract]  
    2.         public interface IMessageExchPatterns  
    3.         {  
    4.             [OperationContract(IsOneWay=false)]  
    5.             string GetDataRequestReply (string name);  
    6. }  
    As shown in the above example you can declare request reply pattern in two ways.

  2. One Way

    In One way pattern client sends the data or request to WCF service but it doesn't wait for any reply from the service and also service doesn't send any reply to them, so here the client will not show any thing on service then will only send the message to service. It doesn't check if that message reaches the service or not. You can set this pattern by IsOneway=true.

    Example
    1. [ServiceContract]  
    2.         public interface IMessageExchPatterns  
    3.         {  
    4.             [OperationContract(IsOneWay=true)]  
    5.             void GetDataOneWay (string name)  
    6. }  
  3. Duplex

    In Duplex pattern you have to predict that there are  two clients (client-1, Client-2); client-1 makes request to service and then service will sent request to client-2 and then service gets response from client-2 and give this replay to client-1.

    Here this pattern is to use the callback method for the internal (client to client ) communication. So here we have to define the callback interface as show below,
    1. [ServiceContract(CallbackContract=typeof(IMessageExchPatternsCallback))]  
    2.         public interface IMessageExchPatterns  
    3.         {  
    4.             [OperationContract(IsOneWay = false)]  
    5.             string GetDataRequestReply(string name);  
    6.   
    7.             [OperationContract(IsOneWay=true)]  
    8.             void GetDataOneWay(string name);  
    9.   
    10.         [OperationContract]  
    11.             void GetDataDuplex(string name);  
    12.         }  
    13.   
    14.         public interface IMessageExchPatternsCallback  
    15.         {  
    16.             [OperationContract]  
    17.             void SendData(string name);  
    18. }  
    As shown in above example we specify the,
    1. [ServiceContract(CallbackContract=typeof(IMessageExchPatternsCallback))]  
    Here we specify the call back contract for the interface. This callback interface (IMessageExchPatternsCallback) is implementing on client side.
    1. [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]  
    2. public class MessageExchPatterns : IMessageExchPatterns  
    3. {  
    4.           
    5.         public string GetDataRequestReply(string name)  
    6.         {  
    7.             return "You have enter : " + name;  
    8.         }  
    9.   
    10.         public void GetDataOneWay(string name)  
    11.         {  
    12.             Console.Write("You have enter : " + name);  
    13.         }  
    14.   
    15.         public void GetDataDuplex(string name)  
    16.         {  
    17.             OperationContext.Current.GetCallbackChannel<IMessageExchPatternsCallback>().SendData("Hi, " + name);  
    18.         }  
    19. }  

Now host this service on console application

  1. ServiceHost host = new ServiceHost(typeof(MessageExchPatterns.MessageExchPatterns));  
  2. host.Open();  
  3. Console.Write("Service started...");  
  4. Console.Read();  
Now create client application with Window form application as below.



Write code as shown below,
  1. [CallbackBehavior(UseSynchronizationContext = false)]  
  2. public partial class Form1 : Form,ServiceReference1.IMessageExchPatternsCallback  
  3.     {  
  4.         public Form1()  
  5.         {  
  6.             InitializeComponent();  
  7.         }  
  8.   
  9.         InstanceContext instanceContext;  
  10.         private void button1_Click(object sender, EventArgs e)  
  11.         {  
  12.              
  13.             ServiceReference1.MessageExchPatternsClient clnt = new ServiceReference1.MessageExchPatternsClient(instanceContext);  
  14.             label1.Text = clnt.GetDataRequestReply(textBox1.Text);  
  15.         }  
  16.   
  17.         void ServiceReference1.IMessageExchPatternsCallback.SendData(string name)  
  18.         {  
  19.             Label3.Text = name;  
  20.         }  
  21.   
  22.         private void button2_Click(object sender, EventArgs e)  
  23.         {  
  24.               
  25. MessageExchPatternsClient clnt = new MessageExchPatternsClient(instanceContext);  
  26.             clnt.GetDataOneWay(textBox2.Text);  
  27.         }  
  28.   
  29.         private void button3_Click(object sender, EventArgs e)  
  30.         {  
  31. ServiceReference1.MessageExchPatternsClient clnt = new ServiceReference1.MessageExchPatternsClient(instanceContext);  
  32.             clnt.GetDataDuplex(textBox3.Text);  
  33.         }  
  34.   
  35.         private void Form1_Load(object sender, EventArgs e)  
  36.         {  
  37.             instanceContext  = new InstanceContext(this);  
  38.         }  
  39.     }  
Now run the WCF service host. Then run client application and check output.

Ebook Download
View all
Learn
View all