OperationContract Attribute And its Parameters In WCF

This blog is an introduction to OperationContract attribute and parameters which can be used with this attribute.
 
OperationContract attribute

[OperationContract] attribute is used to define the methods of service contract. This attribute is placed on methods that you want to include as a part of service contract. Only those methods that are marked with OperationContract attribute are exposed to client. 

Example
  1. [ServiceContract]  
  2.    public interface IStore  
  3.    {  
  4.        [OperationContract]  
  5.        void Login(User u);  
  6.   
  7.        [OperationContract]  
  8.        List<Product> GetProducts(int value);  
  9.   
  10.    }  
For [ServiceContract] attribute, see ServiceContract Attribute And Its Parameters in WCF.

[OperationContract] attribute parameters

[OperationContract] attribute contains parameters that specify additional information about OperationContract attribute. The name of these parameters and their functions are given below.

Action

This parameter gets or sets the WS-Addressing of coming message. This parameter is used to determine which method to send the incoming message to. WCF runtime uses this action to transmit an incoming message to the appropriate method. The default action of this parameter is a combination of the contract namespace, the interface or class name, and the operation name.

Example

  1. [ServiceContract]  
  2.     public interface IStore  
  3.     {  
  4.         [OperationContract]  
  5.         void Login(User u);  
  6.   
  7.         [OperationContract(Action = "OperationContractMethod")]  
  8.         List<Product> GetProducts(int value);  
  9.   
  10.     }  
IsOneWay

This parameter is of bool type and used to specify whether a service operation returns a reply message or not. By default its value is false which indicates that operation does not return a reply message.

Example

  1. [ServiceContract]  
  2.    public interface IStore  
  3.    {  
  4.        [OperationContract(IsOneWay =true)]  
  5.        void Login(User u);  
  6.   
  7.        [OperationContract(Action = "OperationContractMethod")]  
  8.        List<Product> GetProducts(int value);  
  9.   
  10.    }  
IsTerminating

This parameter is used to specify whether the called method terminates the session after reply or not.

Example
  1. [ServiceContract]  
  2.    public interface IStore  
  3.    {  
  4.        [OperationContract(IsOneWay =true)]  
  5.        void Login(User u);  
  6.   
  7.        [OperationContract(Action = "OperationContractMethod")]  
  8.        List<Product> GetProducts(int value);  
  9.   
  10.        [OperationContract(IsTerminating = true)]  
  11.        void Logout(User u);  
  12.   
  13.    }  
Name

This property gets or sets the name of operation. This property is used to override <operation> element in WSDL. The default value is taken from name of method. 
Example
  1. [ServiceContract]  
  2.    public interface IStore  
  3.    {  
  4.        [OperationContract(IsOneWay =true,Name ="UserLogin")]  
  5.        void Login(User u);  
  6.   
  7.        [OperationContract(Action = "OperationContractMethod")]  
  8.        List<Product> GetProducts(int value);  
  9.   
  10.        [OperationContract(IsTerminating = true)]  
  11.        void Logout(User u);  
  12.   
  13.    }   
 AsyncPattern

Service operations can be synchronous and asynchronous, and this parameter is used to specify that the operation is asynchronous. In WCF, asynchronous operations are implemented with Begin<methodname> and End<methodname> pair. This parameter tells the runtime that a Begin method has a corresponding End method. WCF routes incoming messages to the Begin method, and the results of the End method are sent to the outbound message.

Example
  1. [ServiceContract]  
  2.     public interface IStore  
  3.     {  
  4.         [OperationContract(IsOneWay =true,Name ="UserLogin")]  
  5.         void Login(User u);  
  6.   
  7.         [OperationContract(Action = "OperationContractMethod")]  
  8.         List<Product> GetProducts(int value);  
  9.   

  10.         [OperationContract(IsTerminating = true)]  
  11.         void Logout(User u);  
  12.   
  13.         [OperationContract(AsyncPattern = true)]  
  14.         IAsyncResult BeginOrderStatus(string OrderNumber, AsyncCallback callback,object state);  
  15.         void EndOrderStatus(IAsyncResult ar);  
  16.     }   
IsInitiating

The IsInitiating parameter specifies whether or not an operation implemented by the associated method can initiate a session on the server. This property controls whether an operation is allowed to be the first operation called when a session is created. The default value for this parameter is true. If this parameter is set to false, the client is forced to call other methods prior to calling this method.

Example
  1. [ServiceContract]  
  2.     public interface IStore  
  3.     {  
  4.         [OperationContract(IsInitiating=true,IsOneWay =true,Name ="UserLogin")]  
  5.         void Login(User u);  
  6.   
  7.         [OperationContract(IsInitiating = false, Action = "OperationContractMethod")]  
  8.         List<Product> GetProducts(int value);  
  9.   
  10.         [OperationContract(IsInitiating = false, IsTerminating = true)]  
  11.         void Logout(User u);  
  12.   
  13.         [OperationContract(IsInitiating = false, AsyncPattern = true)]  
  14.         IAsyncResult BeginOrderStatus(string OrderNumber, AsyncCallback callback,object state);  
  15.         void EndOrderStatus(IAsyncResult ar);  
  16.     }   
As you  see in the above example, client must call Login method first because its IsInitiating parameter value is true while other method's IsInitiating parameter value is false.

ProtectionLevel

This parameter is used to specify the encryption level of the message of an operation. The value of this parameter can be set by following ProtectionLevel enumeration values. 
 
 ProtectionLevel enumeration values Brief Descriptions
EncryptAndSign Encrypt and Sign data to ensure integrity and confidentiality
 None No Authentication
 Sign Only ensures integrity

Example
  1. [ServiceContract]  
  2.     public interface IStore  
  3.     {  
  4.         [OperationContract(ProtectionLevel =  
  5. System.Net.Security.ProtectionLevel.EncryptAndSign,IsInitiating = true,IsOneWay =true,Name ="UserLogin")]  
  6.         void Login(User u);  
  7.   
  8.         [OperationContract(IsInitiating = false, Action = "OperationContractMethod")]  
  9.         List<Product> GetProducts(int value);  
  10.   
  11.         [OperationContract(IsInitiating = false, IsTerminating = true)]  
  12.         void Logout(User u);  
  13.   
  14.         [OperationContract(IsInitiating = false, AsyncPattern = true)]  
  15.         IAsyncResult BeginOrderStatus(string OrderNumber, AsyncCallback callback,object state);  
  16.         void EndOrderStatus(IAsyncResult ar);  
  17.     }   
ReplyAction

This parameter is used to specify a reply action for an incoming message. The value for this parameter can be a URL or operation name. The default value of this parameter is contract and name of replyaction.

Example
  1.  [ServiceContract]  
  2.     public interface IStore  
  3.     {  
  4.         [OperationContract(ProtectionLevel =  
  5. System.Net.Security.ProtectionLevel.EncryptAndSign,IsInitiating = true,IsOneWay =true,Name ="UserLogin")]  
  6.         void Login(User u);  
  7.   
  8.         [OperationContract(IsInitiating = false, Action = "OperationContractMethod",ReplyAction ="Action Name or URL")]  
  9.         List<Product> GetProducts(int value);  
  10.   
  11.         [OperationContract(IsInitiating = false, IsTerminating = true)]  
  12.         void Logout(User u);  
  13.   
  14.         [OperationContract(IsInitiating = false, AsyncPattern = true)]  
  15.         IAsyncResult BeginOrderStatus(string OrderNumber, AsyncCallback callback,object state);  
  16.         void EndOrderStatus(IAsyncResult ar);  
  17.     }  
Ebook Download
View all
Learn
View all