Blog from Saravanan Arumugam

Let us talk about Technologies

Know the Service and Operation Behaviors


We usually get convinced with the default behavior of a WCF service and its operations. In scenarios where the simple is not sufficient, we tend to set the operation and service behaviors through code based on our need.

Setting a behavior is achieved through ServiceBehaviorAttribute and OperationBehaviorAttribute. The config file is another place for defining Service, Binding and Endpoint behaviors. But in this paper, I limit the discussion to the behaviors that can be set though these two attributes.

These attributes are applicable only on the Service definition not on the service declaration; in other words, use these attributes on the Service’s class definition, not on the Interface.

The typical Service having implemented the behavior attributes would look like this.

    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, 
        TransactionIsolationLevel = IsolationLevel.Serializable, 
        InstanceContextMode = InstanceContextMode.Single, 
        ReleaseServiceInstanceOnTransactionComplete = true)]
    public class BankingService : IBankingService
    {
        [OperationBehavior(ReleaseInstanceMode = ReleaseInstanceMode.None, 
            TransactionScopeRequired = true)]
        public double WithdrawMoney(int accountNumber, double amount) { }
    }

To make a decision on weather the current behavior of Service and operation is sufficient for your service needs or you want to change them, it is important to know the place to check the current behavior during the runtime.

The object useful to examine the behaviors is OperationContext. You can get the current context using OperationContext.Current.

 

Service Behaviors

The following list (screenshot) shows the configurable behaviors of the current service and their default values.

image

The service behavior is extracted with the following statement in Debug mode (with Quickwatch).

OperationContext.Current.Host.Description.Behaviors[typeof(ServiceBehaviorAttribute)]

Note: Besides the Host.Description, the service behavior can be found in various places of OperationContext.Current.EndpointDispatcher.DispatchRuntime. The list of service behaviors found in DispatchRuntime is listed at the end of this paper.

 

OperationBehaviors

The following list (screenshot) shows the configurable behaviors of a specified operation and their default values.

image

The operation behavior for a specific operation contract can be extracted using the following statement.

OperationContext.Current.EndpointDispatcher.DispatchRuntime.Operations["Greeting"]

Note: In the screenshot the IsOneway behavior is displayed as true since I have specified it explicitly. In reality, it’ll be false by default.

List of Service Behaviors found in DispatchRuntime

OperationContext.Current.EndpointDispatcher.DispatchRuntime.ConcurrencyMode

OperationContext.Current.EndpointDispatcher.EndpointAddress

OperationContext.Current.EndpointDispatcher.AddressFilter

OperationContext.Current.EndpointDispatcher.DispatchRuntime.

ReleaseServiceInstanceOnTransactionComplete

OperationContext.Current.EndpointDispatcher.DispatchRuntime.

TransactionAutoCompleteOnSessionClose

OperationContext.Current.EndpointDispatcher.DispatchRuntime.ValidateMustUnderstand

OperationContext.Current.EndpointDispatcher.DispatchRuntime.CallbackClientRuntime

OperationContext.Current.EndpointDispatcher.ContractName

OperationContext.Current.EndpointDispatcher.ContractNamespace

OperationContext.Current.EndpointDispatcher.DispatchRuntime.MessageInspectors

OperationContext.Current.EndpointDispatcher.DispatchRuntime.AutomaticInputSessionShutdown

OperationContext.Current.EndpointDispatcher.DispatchRuntime.SingletonInstanceContext

OperationContext.Current.EndpointDispatcher.DispatchRuntime.SynchronizationContext

OperationContext.Current.EndpointDispatcher.ChannelDispatcher.TransactionIsolationLevel

OperationContext.Current.EndpointDispatcher.ChannelDispatcher.TransactionTimeout

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: