Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Message exchange patterns in WCF

Message exchange pattern describes how client and WCF service exchange message to each other.

You know that WCF data is in XML or JSON format and WCF Client and service exchange messages in XML or JSON format.

 There are three types of exchange patterns :

1. Request/Response
2. One way
3. Duplex

Request-reply pattern:

In the request-reply pattern, a client application sends a message to a WCF service and then waits for a reply. This is the classic and most commonly used message exchange pattern in WCF.

In this exchange pattern if the service doesn't respond to the client then the client will receive a TimeoutException.


Example:

    [ServiceContract(ConfigurationName = "IProductWebService")]
    public interface IEcommerceWebService
    {
 
        /// <summary>
        /// Product price information updating
        /// </summary>
        // specifies the request-reply message pattern
        [OperationContract(IsOneWay=false)]
        bool UpdatePriceList(ProductPrice Price);
      }

 

One-way pattern:

 one-way message exchange pattern, a client application sends a message to a WCF service but the service does not send a reply message to the client. You can use this pattern when a client requests the service take an action but does not need to wait for a reply.

This pattern doesn’t supports output parameters, by-reference parameters and return value to an operation, otherwise you will receive an InvalidOperationException.


Example:

    [ServiceContract(ConfigurationName = "IProductWebService")]
    public interface IEcommerceWebService
    {
       
        /// <summary>
        /// Save new price information
        /// </summary>
        [OperationContract(IsOneWay = true)]
        void SavePriceData(BasicPrice Price);
      }

Duplex pattern:

In the request/reply and one-way message exchange patterns, only the client can initiate communication. In the duplex pattern, both the client and the service can initiate communication. The client calls a method of the service. The service can then use a client callback to call a method in the client. You can use this pattern when you want the service to send a notification or alert to the client after the client has called the service.


Example:

//associate service contract with service contract using call back attribute 
[ServiceContract(CallbackContract = typeof(IProductServiceCallBack))] 
public interface IProductService 

    [OperationContract
    Product GetInfo(); 
}
public interface IProductServiceCallBack 

   [OperationContract]
   void SaveProduct(Product BasicProduct); 

 

Invalid entry,please enter valid data.

Loading