In WCF behaviors are objects that allows you to control and modify the behaviors of WCF service at runtime. There are two types of behaviors in WCF
- Service Behaviors
- Operation Behaviors
Service Behaviors
Service Behaviors specifies the execution behavior of a service contract implementation class and can be defined by just placing [ServiceBehavior] attribute on the class that implements service contract interface. This parameter is optional but if [ServiceBehavior] attribute is used it assist you in specifying behavior of service by providing additional parameters.
Example
- [ServiceContract]
- public interface IServiceClass
- {
- [OperationContract]
- int Square(int number);
-
- }
- [ServiceBehavior]
- public class ServiceClass : IServiceClass
- {
- [OperationBehavior]
- public int Square(int number)
- {
- return number*number;
- }
-
- }
For [ServiceContract] attribute see
ServiceContract Attribute And Its Parameters in WCF
ServiceBehavior attribute contains parameters that assist in specifying the behavior of service. Their name and functions are as follows
AddressFilterMode
This parameter is used to gets or sets the address filter mode of service. This parameter tells the service dispatcher to route incoming message to specific endpoint. The value of this parameter can be set by following AddressFilterMode enumeration values.
AddressFilterMode Enumeration values | Brief Description |
Any | filter matches on any address of incoming message |
Exact | filter does exact match on address of incoming messages |
Prefix | filter matches the longest prefix on address of incoming message |
Example
- [ServiceBehavior(AddressFilterMode=AddressFilterMode = AddressFilterMode.Any)]
- public class ServiceClass : IServiceClass
- {
- ...
- }
The default value of this parameter is exact.
AutomaticSessionShutdown:
This parameter is used to specify whether to close a session automatically when a user closes an output session. By default the value of this parameter is true and service has finished processing after the client close output session.
Example
- [ServiceBehavior(AutomaticSessionShutdown=false)]
- public class ServiceClass : IServiceClass
- {
- ...
- }
ConcurrencyMode
This parameter is used to specify the thread support for service. The value of this property indicates that either the instance of service handle one thread or multiple thread that execute concurrently. The value of this parameter can be set by following ConcurrencyMode enumeration values
ConcurrencyMode Enumeration values | Brief Description |
Single | specify that service instance is single threaded which means that if the first process is being executed than the second process will wait until the first has finished. In this mode service does not except reentrance call |
Multiple | specify that service instance is multi threaded |
Reentrant | specify that service instance is single threaded and can accepts reentrant calls |
Example
- [ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Single)]
- public class ServiceClass : IServiceClass
- {
- ...
- }
The default value for this property is Single.
ConfigurationName
The parameter gets or sets the value that is used to locate the service element in a configuration file. The default value for this property is the namespace qualified name of the type.
Example
- [ServiceBehavior(ConfigurationName=”service”)]
- public class ServiceClass : IServiceClass
- {
- ...
- }
IgnoreExtensionDataObject
This parameter is used to specify that whether to send unknown serialization data between the service and client. In common communication scenarios, most types are defined, and the service knows how to handle each member. For example, the Student type may be defined with ID and Name elements, and the service expects these elements. It is possible, however, to send elements that the service is not aware of, such as Phone Number element. In these cases, any WCF type that implements the IExtensibleDataObject interface stores any extra data sent over. The default value for this property is false
Example
- [ServiceBehavior(IgnoreExtensionDataObject=true)]
- public class ServiceClass : IServiceClass
- {
- ...
- }
IncludeExceptionDetailInFaults
This parameter specifies what is to be done with unhandledexceptions and can be used to send error messages to client for debugging purposes.
Example
- [ServiceBehavior(IncludeExceptionDetailInFaults=true)]
- public class ServiceClass : IServiceClass
- {
- ...
- }
InstanceContextMode
This parameter is used to specify the life time of service object. The value of this parameter can be set by following InstanceContext enumeration values
InstanceContext Enumeration values | Brief Description |
PerCall | New service object is created on each call |
PerSession | New service instance for each new session |
Single | One instance of service for all calls |
Example
- [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
- public class ServiceClass : IServiceClass
- {
- ...
- }
The default value for this property is PerSession. ReleaseServiceInstanceOnTransactionComplete
The parameter is used to specify whether the current service object is recycled when the current transaction is complete. This parameter is of bool type and its default value is true.
Example
- [ServiceBehavior(ReleaseServiceInstanceOnTransactionComplete=true)]
- public class ServiceClass : IServiceClass
- {
- ...
- }
TransactionAutoCompleteOnSessionClose
This parameter is used to specify that whether any pending transactions are to be completed when the current session is closed.
Example:
- [ServiceBehavior(TransactionAutoCompleteOnSessionClose=true)]
- public class ServiceClass : IServiceClass
- {
- ...
- }
The default value for this property is false.
TransactionIsolationLevel
This parameter is used to specify the transaction isolation level of the service which indicates how data is to be treated at run time when there made some changes in other transactions. The value for this parameter are as follows
Chaos | pending changes from more highly visible transactions cannot be overwritten |
ReadCommitted | data that will be affected by a transaction cannot be read during the transaction, but can be modified |
ReadUncommitted | affected data can be read and modified during the transaction |
RepeatableRead | affected data can be read but not modified during transaction. However new data can be added during the transaction |
Serializable | affected data can be read but not modified during the transaction. However no new data can be added during the transaction. |
Snapshot | affected data can be read and current transaction access to previously committed data. |
Unspecified | no updating or inserting can occur until the transaction is complete. |
Example
- [ServiceBehavior(TransactionIsolationLevel=System.Transactions.IsolationLevel.ReadCommitted)]
- public class ServiceClass : IServiceClass
- {
- ...
- }
TransactionTimeout
This parameter is used to specify the time in which a transaction has to complete.
Example
- [ServiceBehavior(TransactionTimeout=”00:01:00”)]
- public class ServiceClass : IServiceClass
- {
- ...
- }
The value of this parameter is in the form of a TimeSpan object and its default value is zero.
UseSynchronizedContext
This parameter specifies whether or not to use the current synchronization context. This parameter is of bool type and its true value specify that all calls to the service will run on the thread specified by the SynchronizationContext.
Example
- [ServiceBehavior(UseSynchronizationContext=true)]
- public class ServiceClass : IServiceClass
- {
- ...
- }
ValidateMustUnderstand
This parameter specifies whether or not the application enforces the SOAP MustUnderstand header processing. This parameter is of bool type and its false value indicates that the application must check for headers tagged with MustUnderstand=”true”. If any of the headers is not understood, a fault message will be returned.
Example
- [ServiceBehavior(ValidateMustUnderstand=false)]
- public class ServiceClass : IServiceClass
- {
- ...
- }
This is all about [ServiceBehavior] attribute and its parameters. In the next blog i will demonstrate
[OperationBehavior] attribute and is parameters.