O'Reilly logo

Programming WCF Services by Juval Lowy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Explicit Transaction Programming

The transactional programming model described so far can only be used declaratively by transactional services. Nonservice clients, nontransactional services, or just plain .NET objects called downstream by a service cannot take advantage of it. For all these cases, WCF relies on the transactional infrastructure available with .NET 2.0 in the System.Transactions namespace. In addition, you may rely on System.Transactions even in transactional services when exploiting some advanced features such as transaction events, cloning, asynchronous commit, and manual transactions. I described the System.Transactions capabilities in my MSDN whitepaper “Introducing System.Transactions in the .NET Framework 2.0” (published April 2005; updated December 2005). The flowing sections contain excerpts from that article describing how to use the core aspects of System.Transactions in the context of WCF. Please refer to the whitepaper for detailed discussions of the rest of the features.

The TransactionScope Class

The most common way of using transactions explicitly is via the TransactionScope class:

public class TransactionScope : IDisposable
{
   public TransactionScope( );
   //Additional constructorspublic void Complete( );
   public void Dispose( );
}

As the name implies, the TransactionScope class is used to scope a code section with a transaction, as demonstrated in Example 7-7.

Example 7-7. Using TransactionScope

using(TransactionScope scope = new TransactionScope( )) { ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required