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.
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
- [OperationBehavior(AutoDisposeParameters = true)]
- public void Square(int number) {
- result = number * number;
- }
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 |
Allowed | Impersonation 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
- [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
- public void Square(int number) {
- result = number * number;
- }
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 values | Brief 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
- [OperationBehavior(ReleaseInstanceMode = ReleaseInstanceMode.AfterCall)]
- public void Square(int number) {
- result = number * number;
- }
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
- [OperationBehavior(TransactionAutoComplete = true)]
- public void Square(int number) {
- result = number * number;
- }
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
- [OperationBehavior(TransactionScopeRequired = true)]
- public void Square(int number) {
- result = number * number;
- }