One-Way Operations in WCF Services

When an operation has no return value, and the client does not care about the success or failure of the invocation. WCF offers one-way operations to support this sort of fire-and-forget invocation,: once the client issues the call, WCF generates a request message, but no correlated reply message will ever return to the client. The one-way message exchange pattern is useful when a client needs to send information to a service but doesn't receive a response. In reality, it's "fire and acknowledge" because the caller receives an acknowledgement that the message was successfully committed to the communication channel.
 
When a client sends a message to a service endpoint whose operation is marked as one-way, control is returned to the caller before the service operation completes. One-way operations are specified on the [OperationContract] attribute by using the IsOneWay=true modifier.
 

Configuring One-Way Operations

 
The OperationContract has a property IsOneWay which is Boolean type.
  1. [AttributeUsage(AttributeTargets.Method)]  
  2. public sealed class OperationContractAttribute : Attribute  
  3. {  
  4.    public bool IsOneWay  
  5.    { getset; }  
  6. }   
Default value of IsOneWay property is false. You need to set IsOneWay to true to configures the method as a one-way operation:
  1. [ServiceContract]  
  2. public interface IArticleService  
  3. {  
  4.    [OperationContract(IsOneWay=true)]  
  5.    void OneWayMethod();  
  6. }

One way Operation in WCF

  1. namespace WCFService  
  2. {     
  3.     [ServiceContract]  
  4.     public interface IArticleService  
  5.     {  
  6.         [OperationContract(IsOneWay=true)]  
  7.         void OneWayMethod();  
  8.   
  9.         [OperationContract]  
  10.         DataTable GetAllArticles();  
  11.     }  
  12. }  
  13.   
  14. namespace WCFService  
  15. {     
  16.     public class ArticleService : IArticleService  
  17.     {  
  18.   
  19.          public void  OneWayMethod()  
  20.          {  
  21.             // Some code here  
  22.             /////////////  
  23.          }  
  24.   
  25.         public DataTable GetAllArticles()  
  26.         {  
  27.             using (var Context = new ArticleDBEntities())  
  28.             {  
  29.                 DataTable objDt = new DataTable();  
  30.                 objDt = Context.Articles.ToList().CopyToDataTable();  
  31.                 return objDt;  
  32.             }  
  33.         }  
  34.     }  
  35. }  
Both service operations in service contract are implemented same but one is marked as one-way operation. When any client calls OneWayMethod the client-side proxy call returns immediately and doesn't wait for the response. When the client calls GetAllArticles the client-side proxy call blocks while the service executes the statement.
 
Client implemented code
  1. ArticleServiceClient proxy = new ArticleServiceClient();  
  2.   
  3. proxy.OneWayMethod();  
  4.   
  5. DataTable objDt = new DataTable();  
  6. objDt = proxy.GetAllArticles();  
  7. proxy.Close();  
You should turn on reliability for your services, even for one-way calls. This will ensure delivery of the requests to the service
 
Sessionful Services with One-Way Operations
 
You can design a sessionful contract with one-way operations:
  1. [ServiceContract(SessionMode = SessionMode.Required)]  
  2. interface IArticleService  
  3. {  
  4.    [OperationContract(IsOneWay = true)]  
  5.    void OneWayMethod();  
  6. }  
If the client issues a one-way call and then closes the proxy while the method executes, the client will still be blocked until the operation completes.


Similar Articles