Wednesday, July 4, 2012

One Way Contract in WCF

In this post, i am going to explain the benefit of the One way contract. I have a page which collect the feedback from the customer. it internally calls web service and send the feed back information to the DB via Wcf service

Created the simple Asp.NET web application which accept the feedback from the users


when the user enter the feedback and click the submit button it's going to call the FeedBackService and the service is not going to return back anything

For demo purpose, in WCF service side, i put thread to sleep for 5 seconds

[ServiceContract]
    public interface IService1
    {
 
        [OperationContract]
        void SendFeedBack(string Name, string Message);
 
       
 
    }


 public class FeedBackService : IService1
    {
        public void SendFeedBack(string Name, String Message)
        {
            Thread.Sleep(5000);
        }
 
        
    }



Let's look at the response time
Http Watch report for the response time

Before


Response time is 5.2 seconds

Adding the OneWay is true in  the service side and updating the proxy class from the client side
Server Side
 [ServiceContract]
    public interface IService1
    {
 
        [OperationContract(IsOneWay=true)]
        void SendFeedBack(string Name, string Message);
 
       
 
    }



Client Side

The following piece of code added part of the proxy

 [System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="http://tempuri.org/IService1/SendFeedBack")]
        void SendFeedBack(string Name, string Message);



After



The response time is comes down to 0.2 seconds


MSDN Description on Oneway call
There are cases when an operation has no returned values and the client does not care about the success or failure of the invocation. To support this sort of fire-and-forget invocation, Windows Communication Foundation offers one-way operations. After the client issues the call, Windows Communication Foundation generates a request message, but no correlated reply message will ever return to the client. As a result, one-way operations can't return values, and any exception thrown on the service side will not make its way to the client. One-way calls do not equate to asynchronous calls. When one-way calls reach the service, they may not be dispatched all at once and may be queued up on the service side to be dispatched one at a time, all according to the service configured concurrency mode behavior and session mode. How many messages (whether one-way or request-reply) the service is willing to queue up is a product of the configured channel and the reliability mode. If the number of queued messages has exceeded the queue's capacity, then the client will block, even when issuing a one-way call. However, once the call is queued, the client is unblocked and can continue executing while the service processes the operation in the background. This usually gives the appearance of asynchronous calls.