Message Exchange Pattern

Message Exchange Pattern is the concept which will show how messages will be exchanged between clients and WCF services.

Type of Message Exchange Pattern

We have three message exchange pattern available in WCF:-

Request & Reply

One-Way

Duplex

Request & Reply is the default message exchange pattern between client and WCF Service.

Request & Reply

In Request & Reply message exchange pattern client send request to the WCF Service and it has to wait for service reply. It doesn’t matter whether your WCF method is going to return some value or not. Even in the situation of void client have to wait for the completion of service method and after that it can perform next operation.

For Example:-

In this example I have created WCF service in which I have created the method in which I put the thread in sleep mode for 5 seconds

[ServiceContract]
public interface IService
{

	[OperationContract]
	void GetData(int value);


}
		

As we know the Request and Reply mode is the default message exchange pattern so we need not add extra code. But if you want to set it then you have to put the following code:-

[ServiceContract]
public interface IService
{

	[OperationContract(IsOneWay=false)]
	void GetData(int value);


}

		

As we can see one interface IService and one Class Service.cs. When we open the wcf service all configuration made with this interface and class. If we will work with the same interface and service we need not to change any configuration. But if we want to make our own interface and class we have to make some changes in configuration file.

Following is the code of Service where we implemented this interface:-

public class Service : IService
{
    public void GetData(int value)
    {
        Thread.Sleep(5000);
        
        
    }

}
	

As you can see that GetData method is void type but still as we set Request & Reply message exchange patter so client will have to wait for the method completion.

Now create the client and call this service

		protected void Page_Load(object sender, EventArgs e)
    {
        ServiceReference1.ServiceClient svc = new ServiceReference1.ServiceClient();
        Response.Write("Time before service calling=" + DateTime.Now);

        svc.GetData(0);

        Response.Write("<br/>Time after service calling=" + DateTime.Now);

    }

		

At client side to check time duration I simply print time before calling the service method and after.

Now execute the code and you will get following output:-

		Request and Reply in WCF
		
		Figure 1
		

. As you can see the time difference is 5 second. Client has to wait in Request & Reply for the complete execution of method.

One Way

In one way request client need not wait for the service to complete its task.

In one way request cline simply make request after that it is service responsibility to execute the method and client can make another request.

Note: - we can apply one way only on the method which is declared as void type.

For example

I am taking the same previous example

[ServiceContract]
public interface IService
{

	[OperationContract(IsOneWay=true)]
	void GetData(int value);


}

Now execute the service and update the service reference at the client side and execute the code

		one way in wcf
		
		Figure 2
		

As you can see in One Way request client need not to wait for the completion of method.

In my next article I will explain the duplex message exchange pattern.

Email Address

For any query you can mail me at Malhotra.isha3388@gmail.com.