Home > Articles > WCF Articles > Message Exchange Patterns in WFC

Message Exchange Patterns in WFC

by Yashwant Kumar   on Oct 15, 2014   Category: WCF  | Level: Intermediate  |  Views: 2336    |  Points: 100   
Like this article? Bookmark and Share:
It describes the ways of communication between client and WCF services.

Introduction

In WCF, The Message Exchange Pattern (MEP) provides a way of communication between client and server. MEP is a beauty of WCF. A services do some task depend on us or send a response to our request. The communication between client and server are done in form of messages. When we send a message as request and get a message as response from the service. The WCF Services support three types of Message Exchange Pattern:

  • Request-Response

  • One-Way

  • Duplex



Request-Response


This is the default message exchange pattern. In this MET communication, client sends the request message to the service and wait for the response message from the service. The default receive timeout period is one minute. If the service does not responded to the client then the client receives a TimeoutException after one minute. In this MET, the client expect a response message even if the the service operation's return type is void. The Request-Response pattern support all the bindings except MSMQ based bindings.

 [ServiceContract]

    public interface IMyService

    {

      [OperationContract]

      string GetName( string name);

      [OperationContract(IsOneWay = false)]

      void AddName(string name);

    }

By default the IsOneWay property is false but for this communication, we need to set IsOneWay property to false explicitly. This communication's operation returns a header with status code 200(OK) and a full SOAP response in the message body.



One-Way

In this MET communication, The client sends request message to the service and does not wait for response message from service. There is one disadvantage of this pattern, the service does not sends back any response message even if any communication error occurs. So, we should use this MET when the service does some operation but does not reply a response. For example, if we want to change the record active status from Inactive to Active and we do not want to get a confirmation message from the service that the record status is changed. We can not use FaultContract with this pattern because FaultContract uses two-way channel. All the WCF bindings support this pattern.

[ServiceContract]

    public interface IMyService

    {

      [OperationContract(IsOneWay = true)]

      void ChangeRecordStatus(string value);

    }

In One-Way communication, we set IsOneWay property of OperationContract attribute to true. This pattern returns only header with the status code 202 (Accepted) and does not return message body.



Duplex

In Duplex MET, the client and services can communicate to each other with using one-way or request-response messages. This pattern use two types of contract, ServiceContract and CallbackContract for its implementation. We can use FaultContract with this pattern because FaultContract uses two-way channel. The all bidirectional capable bindings support duplex pattern.

 [ServiceContract(CallbackContract = typeof(DuplexServiceCallback))]

    public interface IMyService

    {

      [OperationContract(IsOneWay = true)] //one-way message

      void AddName(string name);

      [OperationContract] //request-response message

      string GetName( string name);

    }

We must specified the binding as wsDualHttpBinding in web.config for Duplex pattern. This MET is problematic because it requires a connection to send back to the client and there may be a change that not connect back the client due to firewall and network address translation problems.





Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts