[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
- [ServiceContract]
- public interface IStore
- {
- [OperationContract]
- void Login(User u);
- [OperationContract]
- List<Product> GetProducts(int value);
- }
[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
- [ServiceContract]
- public interface IStore
- {
- [OperationContract]
- void Login(User u);
- [OperationContract(Action = "OperationContractMethod")]
- List<Product> GetProducts(int value);
- }
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
- [ServiceContract]
- public interface IStore
- {
- [OperationContract(IsOneWay =true)]
- void Login(User u);
- [OperationContract(Action = "OperationContractMethod")]
- List<Product> GetProducts(int value);
- }
This parameter is used to specify whether the called method terminates the session after reply or not.
Example
- [ServiceContract]
- public interface IStore
- {
- [OperationContract(IsOneWay =true)]
- void Login(User u);
- [OperationContract(Action = "OperationContractMethod")]
- List<Product> GetProducts(int value);
- [OperationContract(IsTerminating = true)]
- void Logout(User u);
- }
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
- [ServiceContract]
- public interface IStore
- {
- [OperationContract(IsOneWay =true,Name ="UserLogin")]
- void Login(User u);
- [OperationContract(Action = "OperationContractMethod")]
- List<Product> GetProducts(int value);
- [OperationContract(IsTerminating = true)]
- void Logout(User u);
- }
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
- [ServiceContract]
- public interface IStore
- {
- [OperationContract(IsOneWay =true,Name ="UserLogin")]
- void Login(User u);
- [OperationContract(Action = "OperationContractMethod")]
- List<Product> GetProducts(int value);
- [OperationContract(IsTerminating = true)]
- void Logout(User u);
- [OperationContract(AsyncPattern = true)]
- IAsyncResult BeginOrderStatus(string OrderNumber, AsyncCallback callback,object state);
- void EndOrderStatus(IAsyncResult ar);
- }
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
- [ServiceContract]
- public interface IStore
- {
- [OperationContract(IsInitiating=true,IsOneWay =true,Name ="UserLogin")]
- void Login(User u);
- [OperationContract(IsInitiating = false, Action = "OperationContractMethod")]
- List<Product> GetProducts(int value);
- [OperationContract(IsInitiating = false, IsTerminating = true)]
- void Logout(User u);
- [OperationContract(IsInitiating = false, AsyncPattern = true)]
- IAsyncResult BeginOrderStatus(string OrderNumber, AsyncCallback callback,object state);
- void EndOrderStatus(IAsyncResult ar);
- }
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
- [ServiceContract]
- public interface IStore
- {
- [OperationContract(ProtectionLevel =
- System.Net.Security.ProtectionLevel.EncryptAndSign,IsInitiating = true,IsOneWay =true,Name ="UserLogin")]
- void Login(User u);
- [OperationContract(IsInitiating = false, Action = "OperationContractMethod")]
- List<Product> GetProducts(int value);
- [OperationContract(IsInitiating = false, IsTerminating = true)]
- void Logout(User u);
- [OperationContract(IsInitiating = false, AsyncPattern = true)]
- IAsyncResult BeginOrderStatus(string OrderNumber, AsyncCallback callback,object state);
- void EndOrderStatus(IAsyncResult ar);
- }
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
- [ServiceContract]
- public interface IStore
- {
- [OperationContract(ProtectionLevel =
- System.Net.Security.ProtectionLevel.EncryptAndSign,IsInitiating = true,IsOneWay =true,Name ="UserLogin")]
- void Login(User u);
- [OperationContract(IsInitiating = false, Action = "OperationContractMethod",ReplyAction ="Action Name or URL")]
- List<Product> GetProducts(int value);
- [OperationContract(IsInitiating = false, IsTerminating = true)]
- void Logout(User u);
- [OperationContract(IsInitiating = false, AsyncPattern = true)]
- IAsyncResult BeginOrderStatus(string OrderNumber, AsyncCallback callback,object state);
- void EndOrderStatus(IAsyncResult ar);
- }

Join the conversation! Your thoughts help the community grow.