OperationBehavior Attribute And Its Parameters In WCF

OperationBehavior Attribute

OperationBehavior attribute is used to specify the execution behavior of a service operation and placed on the method of service implementation class. Like ServiceBehavior attribute (see ServiceBehavior Attribute for detail), this attribute is also optional.

Example

  1. [ServiceContract]  
  2. public interface IServiceClass {  
  3.     [OperationContract]  
  4.     int Square(int number);  
  5. }  
  6. [ServiceBehavior]  
  7. public class ServiceClass: IServiceClass {  
  8.     [OperationBehavior]  
  9.     public int Square(int number) {  
  10.         return number * number;  
  11.     }  
OperationBehavior attribute parameters

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

AutoDisposeParameters

This parameter is used to specify whether the service will dispose parameters of associated methods automatically or cache them. This parameter is of bool type and its false value indicates that service will cache the parameter resources.

Example

  1. [OperationBehavior(AutoDisposeParameters = true)]  
  2. public void Square(int number) {  
  3.     result = number * number;  
  4. }  

The default value for this property is true.

Impersonation

This parameter specifies the impersonation behavior for a service operation. This parameter lets you specify  the method which is to be executed under the impersonated caller’s identity. The value for this parameter can be set by following ImpersonationOption enumerations values
 
 ImpersonationOption enumerations values Brief Description
 AllowedImpersonation will be performed if credentials are available
 NotAllowed Impersonation will not be performed
 Required Impersonation is required.

If Impersonation is set to either Allowed or Required than a binding must also be used that supports impersonation. Its default value is NotAllowed

Example 

  1. [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]  
  2. public void Square(int number) {  
  3.     result = number * number;  
  4. }  
ReleaseInstanceMode

This parameter is used to determine the recycle point of the service object during the course of an operation. The default behavior of this parameter is according to the InstanceContextMode parameter value of [ServiceContract] attribute. The value of this parameter can be set from the following ReleaseInstanceMode enumeration values

 ReleaseInstanceMode enumeration valuesBrief Description 
 AfterCall

Recycles the object after the operation has completed

 BeforeAndAfterCall Recycles the object before calling the operation and after operation completion
 BeforeCall Recycles the object before calling the operation
 None Recycles the object according to the InstanceContextMode

Example 

  1. [OperationBehavior(ReleaseInstanceMode = ReleaseInstanceMode.AfterCall)]  
  2. public void Square(int number) {  
  3.     result = number * number;  
  4. }  

The default value for this parameter is None.

TransactionAutoComplete

This parameter specify whether to complete current transaction automatically or cancelled in case if exceptions are found.

Example 

  1. [OperationBehavior(TransactionAutoComplete = true)]  
  2. public void Square(int number) {  
  3.     result = number * number;  
  4. }  
TransactionScopeRequired

This parameter specifies whether the associated method requires a transaction scope. In case of flowed transaction (a transaction in which transaction id is passed over the wire and used on the receiving side to perform some task), the method will execute within that transaction, otherwise a new transaction is created and used for that method execution.

Example 
  1. [OperationBehavior(TransactionScopeRequired = true)]  
  2. public void Square(int number) {  
  3.     result = number * number;  
  4. }