A simple class to help hooking up System.Transactions and the transacted file system in Longhorn

In a previous note I showed using System.Transactions with the Longhorn transacted file system.  That note had explicit calls to the Win32 services EnterTransactionScope and ExitTransactionScope.  That worked to help show the example, but I want to share a small class that helps simplify the application code.

 

So, consider the following sample:

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Transactions;

 

 

namespace WindowsVistaSample

{

    class ActiveKernelResourceRegion : IDisposable

    {

        [System.Runtime.InteropServices.DllImport("kernel32")]

        internal static extern bool EnterTransactionScope();

 

        [System.Runtime.InteropServices.DllImport("kernel32")]

        internal static extern bool ExitTransactionScope();

 

        private TransactionScope scope;

        private bool enteredKernelAmbient = false;

        private bool disposed = false;

 

        public ActiveKernelResourceRegion()

        {

            if (Transaction.Current == null)

            {

                throw new InvalidOperationException

                          ("KernelAmbient requires an existing ambient transaction");

            }

 

            this.scope = new TransactionScope(TransactionScopeOption.Required,

                                              new TransactionOptions(),

                                              EnterpriseServicesInteropOption.Full);

            if (!EnterTransactionScope())

            {

                throw new TransactionException

                          ("Unable to enter the kernel ambient region");

            }

            this.enteredKernelAmbient = true;

        }

 

        public void Dispose ()

        {

            if (!this.disposed)

            {

                if (this.enteredKernelAmbient)

                {

                    this.enteredKernelAmbient = false;

                    if (!ExitTransactionScope())

                    {

                        throw new Exception

                                  ("Fatal error occurred attempting " +

                                   "to exit a kernel ambient region");

                    }

                }

 

                if (this.scope != null)

                {

                    this.scope.Complete();

                    this.scope.Dispose();

                    this.scope = null;

                }

 

                this.disposed = true;

            }

        }

    }

}

 

This class is used to denote a region where the file and registry operations have been attached to the ambient System.Transactions transaction.  The following code shows using it to include a file operation within an active TransactionScope:

 

using (TransactionScope scope = new TransactionScope())

{

    using (new ActiveKernelResourceRegion ())

    {

        StreamWriter stream;

 

        using (stream = File.CreateText(fileName1))

        {

            stream.WriteLine(content);

        }

 

     }

 

     scope.Complete();

}

 

UPDATE: This approach is no longer appropriate as of the Vista RC update.  See http://pluralsight.com/blogs/jimjohn/archive/2006/08/31/36819.aspx for more information.


Posted Sep 13 2005, 06:46 PM by jim-johnson
Filed under:

Comments

Sahil Malik wrote re: A simple class to help hooking up System.Transactions and the transacted file system in Longhorn
on 09-14-2005 6:34 AM
Some guidelines around using Transactional filesystem versus otherwise would be helpful. I am sure TxFs has it's uses and advantages but it must have some downsides as well.

Also, EnterTransactionScope and ExitTransactionScope <--- are these brand new in Windows Vista? Any chance they will be backported to other versions of Windows?
Jim Johnson wrote re: A simple class to help hooking up System.Transactions and the transacted file system in Longhorn
on 09-14-2005 4:07 PM
Sahil,

Thanks for your questions.

The second question is the easiest. EnterTransactionScope and ExitTransactionScope are Win32 calls that are new in Windows Vista (nee' Longhorn). While they are part of the kernel transaction management work, they matter only because of the transactional file system and registry. I would be very surprised if either of those features were to be backported to any earlier version of Windows.

The first question is somewhat more complicated. Like any use of transactions, there are tradeoffs to consider. The fact that the data is stored in a file doesn't change the concerns you'd have with transactions anyway: how long can data be locked before it is a problem? Can you reasonably handle in doubt transactions? Do you have the storage for the recovery information?

In addition, there are some specific items to think about around using the transacitonal file system. That's a bit more complicated, though, and I'll put that into its own note -- hopefully tomorrow.
Sahil Malik wrote re: A simple class to help hooking up System.Transactions and the transacted file system in Longhorn
on 09-14-2005 7:13 PM
Thank you for your answers Jim. I've learnt a lot from your meaningful blogposts (unlike mine LOL). :)

I'll keep my eyes peeled for your coming blogposts. :)
Sahil Malik [MVP C#] wrote Ssytem.Transactions: Judicious use of Transactional FileSystem in Windows Vista
on 09-15-2005 3:07 PM
So if you've been following my blog for a while, you'd know that I have a dedicated category for System.Transactions&nbsp;....
Scott Galloway's Personal Blog wrote Some really nice posts on System.Transactions
on 09-16-2005 5:17 AM
I've posted about this namespace in the past, and its no secret I think this is a great feature in .NET...
PuntoRete wrote Transactional File System in Vista
on 12-11-2005 7:09 AM
Christian Weyer wrote re: A simple class to help hooking up System.Transactions and the transacted file system in Longhorn
on 01-22-2006 12:32 PM
Jim,

do we need to enable anything in Vista 5270 to get this working...? I always get back a 'false' when calling EnterTransactionScope.
Thanks.

Christian
Jim Johnson wrote re: A simple class to help hooking up System.Transactions and the transacted file system in Longhorn
on 01-22-2006 6:03 PM
Christian,

I wouldn't have thought so. I don't have a Vista machine with me just now -- I'll check on it tomorrow and get back to you.

Jim.
Jim Johnson wrote re: A simple class to help hooking up System.Transactions and the transacted file system in Longhorn
on 01-23-2006 9:25 AM
Christian,

This is not expected. Could you send me offline the error code from GetLastError() when the EnterTransactionScope call fails, and the contents of the trace file %windir%\system32\msdtc\trace\ktmrmtrace.log, if one exists?

Thanks,
Jim.
Builder wrote re: A simple class to help hooking up System.Transactions and the transacted file system in Longhorn
on 02-20-2006 5:20 AM
I 've run the code on Vista 5270, no similar error with me, make sure to take the code exactly as it is here.
JFG wrote re: A simple class to help hooking up System.Transactions and the transacted file system in Longhorn
on 06-06-2006 1:07 PM
Same here with build 5384. Configuration problem perhaps?
JFG wrote re: A simple class to help hooking up System.Transactions and the transacted file system in Longhorn
on 06-08-2006 3:53 AM
Hi,

I have successfully run your example.

What I am attempting to do is to opening a file once only, write into it with multiple transactions, and then close it.

I have not been able to set this to work yet. Can it be done? OR Do I have to Open , modify and close the file all within a single transaction?
Jim Johnson wrote re: A simple class to help hooking up System.Transactions and the transacted file system in Longhorn
on 06-08-2006 7:32 AM
This is expected behavior. You have to open, modify, and close the file within a single transaction. You cannot open the file, then run a sequence of transactions against the open file, and then close it.

Once a transaction commits or rolls back any file or registry handle that was involved in the transaction goes to a state where you can only close it.

Jim.
Daniel Moth wrote using pattern
on 07-06-2006 4:33 AM
using pattern
Meble wrote re: A simple class to help hooking up System.Transactions and the transacted file system in Longhorn
on 02-07-2007 8:11 AM
This is very helpful code but on Vista not work :(
Someone have solution for this windows?
Thanks
Jim Johnson wrote re: A simple class to help hooking up System.Transactions and the transacted file system in Longhorn
on 02-07-2007 8:14 AM
There were some late changes in the TxF API. Please look at the updated article at http://pluralsight.com/blogs/jimjohn/archive/2006/08/31/36819.aspx.

Jim.
przepisy kulinarne zupy wrote re: A simple class to help hooking up System.Transactions and the transacted file system in Longhorn
on 06-27-2008 3:03 AM

This is interesting article, I did not it think that it yes. Interesting it knew persons about this how much. Sorry if I wrote bad there now my English is novice and I do not it write yet good.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?