Simple Steps to Enable Transactions in WCF

A transaction is basically a logical unit of work consisting of activities that must all succeed or fail and that must comply 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 to).

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 to consider.

Now, consider we have already developed a WCF service and we need to enable transactions in it. So, we will use the steps below:

1. Add the System.Transactions namespace to the WCF Service project.

2. Set the TransactionFlow property of the OperationContract attribute to Mandatory.

Available options for TransactionFlow are:

  • Mandatory - transaction must be flowed
  • Allowed - transaction may be flowed
  • Not Allowed - transaction is not flowed

For example, our WCF service contract is 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, the transaction will be committed.

4. Enable Transactions for WCF Binding being used.

For example, in our configuration file the 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. A transaction from the client must be started 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 serviceBehavior attribute 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.

There are a few more things regarding "Service Instancing and Sessions" that need to be considered while working with Transactions but this WCF tutorial is more focused on enabling transactions in a simple scenario. In my later WCF article on Windows Communication Foundation Transactions on this blog, I'll discuss those concepts in more details.

Other WCF Service articles that might be of your interest:


Similar Articles