Tuesday, August 20, 2013

Windows Communication Foundation Transactions Overview

Transactions provide a way to group a set of actions or operations into a single indivisible unit of execution. A transaction is a collection of operations with the following properties:

Atomicity : This ensures that either all of the updates completed under a specific transaction are committed and made durable or they are all aborted and rolled back to their previous state.

Consistency : This guarantees that the changes made under a transaction represent a transformation from one consistent state to another. For example, a transaction that transfers money from a checking account to a savings account does not change the amount of money in the overall bank account.

Isolation : This prevents a transaction from observing uncommitted changes belonging to other concurrent transactions. Isolation provides an abstraction of concurrency while ensuring one transaction cannot have an unexpected impact on the execution of another transaction.

Durability : This means that once committed, updates to managed resources (such as a database record) will be persistent in the face of failures.

Windows Communication Foundation (WCF) provides a rich set of features that enable you to create distributed transactions in your Web service application.

WCF implements support for the WS-AtomicTransaction (WS-AT) protocol that enables WCF applications to flow transactions to interoperable applications, such as interoperable Web services built using third-party technology. WCF also implements support for the OLE Transactions protocol, which can be used in scenarios where you do not need interop functionality to enable transaction flow.

You can use an application configuration file to configure bindings to enable or disable transaction flow, as well as set the desired transaction protocol on a binding. In addition, you can set transaction time-outs at the service level using the configuration file.

Transaction attributes in the System.ServiceModel namespace allow you to do the following:
  1. Configure transaction time-outs and isolation-level filtering using the ServiceBehaviorAttribute attribute.
  2. Enable transactions functionality and configure transaction completion behavior using the OperationBehaviorAttribute attribute. 
  3. Use the ServiceContractAttribute and OperationContractAttribute attributes on a contract method to require, allow or deny transaction flow.
How to Configure Transaction in WCF?

There are some factors that need to be considered while configuring a transaction in WCF.

Binding :There are only few bindings in WCF that can support transaction, these are




, and


So, in order to configure transaction, one has to make a choice out of such bindings. Though these bindings do have transaction support but by default it is disabled, so one has to make them enabled.

Operation Contract and behavior : An appropriate binding merely makes a way to propagate transaction from client to service but at service there is only an operation/method that is going to take care of that. So, the next step is to configure an operation. Operations are configured generally with the following two attributes:

A) TransactionFlow : This attribute is set with a parameter named TransactionFlowOption which has three values. Its values are self descriptive:
  1. Allowed – Implies if client has transaction, it will be accepted otherwise not a problem.
  2. NotAllowed – Client cannot send transaction to server while calling a method.
  3. Mandatory – Client has to create a transaction otherwise the method cannot be called.
B) TransactionScopeRequired : This OperationBehavior depicts whether the method/operation should do its work inside a transaction

While developing a Service Contract you have to specify the TransactionFlow attribute on each
Operation Contracts that requires a Transaction to be handled. In the below code snippet there are two methods one which submit employee master information and the other that submits employee details. As both require transaction so I have specified TransactionFlow attribute on both of them.

Transaction is basically a logical unit of work comprising of activities that all needed to be succeeded or failed, and also it must be compliant with ACID principals.

Movement of money from a bank account to another is a simple example of a transaction. In this single transaction, two operations will be performed. One account will be debited (amount will be 
taken from) and other will be credited (amount will be deposited).

Enabling transactions in Windows Communication Foundation is simple and straight forward but implementation sometimes becomes difficult depending upon the scenario. For example, implementing transactions in a distributed environment will definitely require effort and more things to consider.

Now, consider we already have developed a WCF service and we wanted to enable transactions on it. So, we will follow the steps below:

  1. Add System.Transactions namespace to WCF Service project.
  2. Set TransactionFlow property of the OperationContract attribute to Mandatory.  Available options for TransactionFlow are: a. Mandatory - transaction must be flowed b. Allowed - transaction may be flowed c. Not Allowed - transaction is not flowed For example, our WCF service contract as follows: [TransactionFlow(TransactionFlowOptions.Mandatory] void MyMethod();
  3. Now, set the OperationBehavior attribute for the implementing method. [OperationBehavior(TransactionScopeRequired=true, TransactionAutoComplete=true)] void MyMethod() { } TransactionScopeRequired = true means it can only be called in a transaction. TransactionAutoComplete = true means that if the operation completes successfully, transaction will be committed. 
  4. Enable Transactions for WCF Binding being used. For Example, In our configuration file bindings will be as follows: <bindings>   <wsHttpBinding>      <binding name=”httpBinding”  transactionFlow=”true” />   </ wsHttpBinding > </bindings>  Remember that we must choose a binding that supports transactions i.e. netTcpBinding, netNamedPipeBinding, wsHttpBinding, wsDualHttpBinding, and wsFederationHttpBinding.
  5. Need to start the transaction from client as: using System.Transaction; Using( var transScope = new TransactionScope()) {          //Calling service methods      IMyServiceClient client = new IMyServiceClient();      client.MyMethod();            transScope.complete();      }
Optionally, If we wanted to specify the Isolation level for the transaction, we can add serviceBehaviorattribute to implementing class as follows: [ServiceBehavior(TransactionIsolationLevel=System.Transaction.IsolationLevel.Serializable)] Public class MyService : IMyService{}  This is all we need to do for enabling transactions in WCF.
Post a Comment