Manually creating DTC Transactions

I have seen this article somewhere before. I am not able to find it. I modified the code little bit and reposting it here.

Scenario: There are cases where your application has to update your database and queue messages to MSMQ in a single atomic transaction scope. In .NETV1.0, the only way to achieve this is to use COM+ through System.EnterpriseServices. In .NET V1.1 there is another way to do this without using System.EnterpriseServices.

The idea here is to create a DTC transaction through TransactionDispenser component provided by DTC. Once you get the transaction object, you can pass the transaction object to your SQL provider as well as MSMQ APIs to participate in a single transaction. DTC takes care of two phase commit.

Sample code here explains how to obtain a transaction from DTC. Once you get the ITransaction interface, you can pass it on to any resource managers that support ITransaction. You have to use Interop to get the DTC Transaction.

using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.EnterpriseServices;

namespace Transactions

  ISOLATIONLEVEL_CHAOS            = 0x00000010,
  ISOLATIONLEVEL_BROWSE           = 0x00000100,
  ISOLATIONLEVEL_ISOLATED         = 0x00100000

 public enum ISOFLAG
  ISOFLAG_READONLY          = 32

 //0x3A6AD9E1, 0x23B9, 0x11cf, 0xAD, 0x60, 0x00, 0xAA, 0x00, 0xA7, 0x4C, 0xCD);
 public interface ITransactionDispenser
  ushort GetOptionsObject([MarshalAs(UnmanagedType.IUnknown)]ref object options);

  int BeginTransaction(
   [MarshalAs(UnmanagedType.IUnknown)] object pUnknownOuter,
   ISOFLAG isoFlag,
   [MarshalAs(UnmanagedType.IUnknown)] object transactionOptions,
   [MarshalAs(UnmanagedType.Interface)] ref ITransaction pTransaction);

 public class NativeSafeDTC
  public static Guid DispenserIID = new Guid("3A6AD9E1-23B9-11cf-AD60-00AA00A74CCD");

  public static extern int DtcGetTransactionManager(
    IntPtr hostName,//[MarshalAsAttribute(UnmanagedType.LPStr)]string hostName,
    IntPtr tmName,//[MarshalAsAttribute(UnmanagedType.LPSTR)]string tmName,
    ref Guid iid,
    UInt32 dwReserved1,
    UInt16 wReserved2,
    IntPtr pvReserved,
    [MarshalAsAttribute(UnmanagedType.Interface)]ref ITransactionDispenser txnDispenser);

Client Code

class Class1
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  static void Main(string[] args)
   // TODO: Add code to start application here

   ITransactionDispenser oDispenser = null;

   int hResult = NativeSafeDTC.DtcGetTransactionManager(IntPtr.Zero,//null,
         ref NativeSafeDTC.DispenserIID,
         0, 0, IntPtr.Zero,
         ref oDispenser);
   if (oDispenser != null)
    ITransactionDispenser dispenser = oDispenser;

    Console.WriteLine("Got a good dispenser");

    for (int i=0; i < 100000; i++)
     ITransaction transaction = null;
      ISOFLAG.ISOFLAG_RETAIN_DONTCARE, null, ref transaction);
     if (transaction != null)
      //Console.WriteLine("Obtained good transaction object");


Comments (7)
  1. chadb says:

    Does this work on XP also? Or just 2003 server?

  2. ramkoth says:

    I have tested this on XP workstation. Not sure about Home edition. You also need .NET v1.1

  3. What would be the benefit of not using enterprise services to handle this transaction? Thanks!

  4. ramkoth says:

    I believe it should work on Win2K as well even though i haven’t tested it.

  5. ramkoth says:


    There are cases where you simply don’t want your components to be registered as COM+ components as things such as deployment etc will become little more involved. You may also want to consider perf implications of configuring component as COM+ (especially, if you run out of process).

    I am not saying that you don’t need COM+ for distributed transactions. But if your scenario mandates that your component should not be COM+ configured, then this approach would be helpful.

  6. Ahhhh I see now. Using your code example we can avoid the need to either manually/lazy register the component into COM+ and avoid that hassel. Thanks for the tip!

  7. Ralf says:

    What you left out in your example is enlisting a ADO.NET connection in the manually created DTC tx, e.g.

    SqlConnection conn = new SqlConnection("…");



    Even though this is easy, I´d prefer to get connections automatically enlisted in the/a running transaction, like it happens within a ServicedComponent. How can accomplish that?

Comments are closed.

Skip to main content