Message Exchange Patterns in WCF

By | September 29, 2014

In Windows Communication Foundation, both parties (i.e. a service and client) communicate with each other and exchange messages using a certain pattern known as Message Exchange Pattern (MEP). WCF supports three different types of MEPs (Message Exchange Patterns).

  • Request-Reply
  • One-Way
  • Duplex

In this WCF Tutorial Series, we will explore all three Message Exchange Patterns step by step with the help of sample code.

Request-Reply Message Exchange Pattern:

Windows Communication Foundation uses Request-Reply as default pattern for communication. In this pattern, when a service operation is called, it always generate a response message to caller (i.e. a client). The client will receive a response even if the service operation has void return type (receiving an empty SOAP body in this case).

A client always waits for a reply from service for certain time (i.e. ReceiveTimeout) and then execute the following piece of code. Default value for ReceiveTimeout is one minute, after that it throws an exception of type TimeoutException.WCF Request-Reply MEP

Almost all WCF Bindings support Request-Reply MEP except the followings:

  • NetMsmqBinding
  • NetPeerTcpBinding

Because Request-Reply is the default Message Exchange Pattern in WCF, so we don’t need to do anything to enable it. But for the purpose of understanding, we can explicitly set it enabled as follows:

[ServiceContract]
public interface RequestReplyMEPService
{
           [OperationContract(IsOneWay = false)]
           void SaveData(string value);
}

One-Way Message Exchange Pattern:

In certain scenarios, when we are not interested to receive response message back, instead just wanted to execute a certain business functionality by sending a message to WCF service. For example, we wanted to execute a long running business process or want queued message delivery. WCF provides One-Way Message Exchange Pattern (MEP) to fulfill such scenarios.WCF One-Way MEP

In One-Way MEP, client doesn’t need to wait for response after sending message to WCF service. Even if some error occurs in communication, WCF Service doesn’t send any response back. That’s why One-Way Message Exchange Pattern provides no support for followings in service operations:

  • no output parameters
  • no reference parameters
  • no return value

All WCF bindings support One-Way Message Exchange Pattern. In order to enable One-Way Message Exchange Pattern in WCF, do the follows:

[ServiceContract]
public interface OneWayMEPService
{
           [OperationContract(IsOneWay = true)]
           void SaveData(string value);
}

Duplex Message Exchange Pattern:

Duplex “as it’s name indicates” is a two-way message channel. In certain scenarios, we want to get a confirmation notification back from WCF service as an operation completes at service end. In more simple words, we can say that both service as well as client can send messages to eachother when using Duplex MEP. So, this message sending in duplex pattern can be request-reply (synchronous) or one-way (asynchronous).

Below diagrams further enhances the concept of Duplex MEP.Duplex (Request-Reply) MEP

Duplex (One-Way) MEP

In order to implement Duplex pattern for message exchange, CallbackContract is additionally required along with ServiceContract. We need to associate CallbackContract with ServiceContract through CallbackContract property of our ServiceContract.

For example, we have a CallbackContract named “DuplexMEPServiceCallback” as follows:

//CallbackContract
  public interface DuplexMEPServiceCallback
  {
          [OperationContract(IsOneWay = true)]
          void ProcessComplete(int value);
  }

So, we will associate above CallbackContract to following ServiceContract named “DuplexMEPService” through CallbackContract property as follows:

  [ServiceContract(CallbackContract = typeof(DuplexMEPServiceCallback))]
  public interface DuplexMEPService
  {
           [OperationContract(IsOneWay = true)]
           void CallProcess(string value);
  }

Duplex MEP being a two-way message channel works with few bindings that has bi-directional capabilities like wsDualHttpBinding. So, endpoint will be defined
as follows:

<endpoint address=”” binding=”wsDualHttpBinding” contract=”DuplexMEPService”>

There are always some concerns while using Duplex MEP that must be considered or addressed as:

  • Maintaining a long-running session between parties can create scalability problems.
  • It requires a connection back to client which is always not possible.

We have discussed in details about all three Message Exchange Pattern in this WCF Tutorial with respective scenarios. Your feedback is really important for me. You can leave your feedback about how fruitful the content is? Enjoy WCF Service Programming.

To test and further enhance your WCF skillsWCF Online Test

Other WCF and related Tutorials:

Top 10 Interview Questions and Answers Series:

Category: Uncategorized

About IMRAN ABDUL GHANI

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.