ServiceBehavior Attribute And Its Parameters In WCF

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

  1. Service Behaviors
  2. 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
  1. [ServiceContract]  
  2.  public interface IServiceClass  
  3.  {  
  4.      [OperationContract]  
  5.      int Square(int number);  
  6.      
  7.  }  
  8.  [ServiceBehavior]  
  9.  public class ServiceClass : IServiceClass  
  10.  {  
  11.      [OperationBehavior]  
  12.      public int Square(int number)  
  13.      {  
  14.          return number*number;  
  15.      }  
  16.     
  17.  }  
 For [ServiceContract] attribute see ServiceContract Attribute And Its Parameters in WCF
 For [OperationContract] attribute see OperationContract Attribute And its Parameters In WCF 

[ServiceBehavior] attribute parameters

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
 Anyfilter matches on any address of incoming message
 Exactfilter does exact match on address of incoming messages
 Prefix filter matches the longest prefix on address of incoming message

Example 
  1. [ServiceBehavior(AddressFilterMode=AddressFilterMode = AddressFilterMode.Any)]  
  2. public class ServiceClass : IServiceClass  
  3. {  
  4. ...  
  5. }  
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 

  1. [ServiceBehavior(AutomaticSessionShutdown=false)]  
  2. public class ServiceClass : IServiceClass  
  3. {  
  4. ...  
  5. }  
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
 Singlespecify 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
  1. [ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Single)]  
  2. public class ServiceClass : IServiceClass  
  3. {  
  4. ...  
  5. }  
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 
  1. [ServiceBehavior(ConfigurationName=”service”)]  
  2. public class ServiceClass : IServiceClass  
  3. {  
  4. ...  
  5. }  
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 
  1. [ServiceBehavior(IgnoreExtensionDataObject=true)]  
  2. public class ServiceClass : IServiceClass  
  3. {  
  4. ...  
  5. }  
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 
  1. [ServiceBehavior(IncludeExceptionDetailInFaults=true)]  
  2. public class ServiceClass : IServiceClass  
  3. {  
  4. ...  
  5. }  
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
PerCallNew service object is created on each call

PerSession

 New service instance for each new session
Single One instance of service for all calls

Example
  1. [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]  
  2. public class ServiceClass : IServiceClass  
  3. {  
  4. ...  
  5. }  
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

  1. [ServiceBehavior(ReleaseServiceInstanceOnTransactionComplete=true)]  
  2. public class ServiceClass : IServiceClass  
  3. {  
  4. ...  
  5. }  
TransactionAutoCompleteOnSessionClose

This parameter is used to specify that whether any pending transactions are to be completed when the current session is closed.

Example: 
  1. [ServiceBehavior(TransactionAutoCompleteOnSessionClose=true)]  
  2. public class ServiceClass : IServiceClass  
  3. {  
  4. ...  
  5. }  
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 
  1. [ServiceBehavior(TransactionIsolationLevel=System.Transactions.IsolationLevel.ReadCommitted)]  
  2. public class ServiceClass : IServiceClass  
  3. {  
  4. ...  
  5. }  
TransactionTimeout

This parameter is used to specify the time in which a transaction has to complete.

Example 

  1. [ServiceBehavior(TransactionTimeout=”00:01:00”)]  
  2. public class ServiceClass : IServiceClass  
  3. {  
  4. ...  
  5. }  
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 

  1. [ServiceBehavior(UseSynchronizationContext=true)]  
  2. public class ServiceClass : IServiceClass  
  3. {  
  4. ...  
  5. }  
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
  1. [ServiceBehavior(ValidateMustUnderstand=false)]  
  2. public class ServiceClass : IServiceClass  
  3. {  
  4. ...  
  5. }  
This is all about [ServiceBehavior] attribute and its parameters. In the next blog i will demonstrate
[OperationBehavior] attribute and is parameters.
Ebook Download
View all
Learn
View all