Click here to Skip to main content
13,139,254 members (86,251 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked
Posted 7 Aug 2012

Simple Steps to Enable Transactions in WCF

, 26 May 2014
Rate this:
Please Sign up or sign in to vote.
Here are some simple steps to enable transactions in WCF

Transaction is basically a logical unit of work comprising 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:

    1. Mandatory - transaction must be flowed
    2. Allowed - transaction may be flowed
    3. Not Allowed - transaction is not flowed

    For example, our WCF service contract as follows:

    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:

         <binding name="httpBinding"  transactionFlow="true"/>
      </ wsHttpBinding >

    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();

Optionally, if we wanted to specify the Isolation level for the transaction, we can add serviceBehavior attribute to implementing class as follows:

Public class MyService : IMyService{}

This is all we need to do for enabling transactions in WCF. There are 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 simple scenario. In my later WCF article on Windows Communication Foundation Transactions on this blog, I'll discuss those concepts in more details.

Other Top WCF Tutorials


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Imran Abdul Ghani
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 26 May 2014
Article Copyright 2012 by Imran Abdul Ghani
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid