Export (0) Print
Expand All

How to: Use the BYOT (Bring Your Own Transaction) Feature of COM+

[Note: This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Code Example

The following procedure and example show how a class derived from the ServicedComponent class can use the BYOT feature of COM+ to access a Distributed Transaction Coordinator (DTC). For more on using the BYOT feature from within the System.EnterpriseServices namespace, see BYOT (Bring Your Own Transaction).

To create an object that uses the transaction from another object.

  1. Define a class that derives from the ServicedComponent class, and exposes a public property that returns the current COM+ DTC Transaction object. For example, in the following code the class Base, inherits the ServicedComponent class and has a public property that returns the ContextUtil.Transaction object for the current COM+ DTC transaction in which the object is instantiated.

    Imports System.EnterpriseServices
    Public Class Base 
          Inherits ServicedComponent
          Public Readonly Property Transaction() as Object
            Get
                Return ContextUtil.Transaction
            End Get
          End Property
    End Class
    

    using System.EnterpriseServices;
    public class Base : ServicedComponent
    {
        public Object Transaction 
        { 
            get { return ContextUtil.Transaction; } 
        }    
    }
    
    
  2. Create two classes that derive from the class defined in step 1 and set the Transaction attribute for both classes. For example, the following code declares two classes CTransaction1 and CTransaction2 that derive from the class Base, and have their Transaction attribute set.

    <Transaction()> Public Class CTransaction1 
    Inherits Base
    ' Insert your transaction logic here.
    End Class
    
    <Transaction()> Public Class CTransaction2 
    Inherits Base
    ' Insert your transaction logic here.
    End Class
    

    [Transaction]
    public class CTransaction1 : Base 
    {
          // Insert your transaction logic here.
    }
    
    [Transaction]
    public class CTransaction2 : Base 
    {
          // Insert your transaction logic here.
    }
    
    
  3. Create a client application that uses the classes created in step 2 to test the BYOT feature.

    1. Create an instance of one of the classes defined in step 2. For example, the following code creates an instance of the CTransaction1 class.

      Class BYOTDemonstration
          Public Shared Sub Main()
              Dim tx1 as New CTransaction1
          End Sub
      End Class
      

      class BYOTDemonstration
      {
          static void Main()
          {
              CTransaction1 tx1 = new CTransaction1();
          }
      }
      
      
    2. Retrieve the current COM+ DTC transaction from the object created in step 3.a. For example, the following code retrieves the Transaction object from the public property Transaction of the CTransaction1 class.

      Dim tx as Object = tx1.Transaction
      

      Object tx = tx1.Transaction;
      
      
    3. Create an instance of the second class defined in step 2, by using the Transaction object retrieved in step 3.b. For example, the following code creates an instance of the class CTransaction2 by using the Transaction retrieved in step 3.b.

      Dim tx2 as CTransaction2 = ctype(BYOT.CreateWithTransaction(tx, _ gettype(CTransaction2)),CTransaction2)
      

      CTransaction2 tx2 = (CTransaction2)BYOT.CreateWithTransaction(tx, 
                           typeof(CTransaction2));
      
      
    4. Generate a strong key and compile the example as follows:

      sn –k BYOTDemo.snk
      vbc /r:System.EnterpriseServices.dll BYOTDemo.vb
      

      sn –k BYOTDemo.snk
      

using System;
using System.Reflection;
using System.EnterpriseServices;

[assembly: AssemblyKeyFileAttribute("byotdemo.snk")]

public class Base : ServicedComponent
{
    public Object Transaction 
    { 
        get { return ContextUtil.Transaction; } 
    }    
}

[Transaction]
public class CTransaction1 : Base 
{
      // Insert your transaction logic here.
}

[Transaction]
public class CTransaction2 : Base 
{
      // Insert your transaction logic here.
}

class BYOTDemonstration
{
      static void Main()
      {
            /* Create a transactional object, and then get its
                  transaction. */
              CTransaction1 tx1 = new CTransaction1();
            Console.WriteLine("Created transaction1.");
              Object tx = tx1.Transaction;
            Console.WriteLine("Got the transaction of transaction1.");        
              CTransaction2 tx2 = (CTransaction2)BYOT.CreateWithTransaction(tx, typeof(CTransaction2));
            Console.WriteLine("Created transaction2 using the transaction of transaction1.");
      }
}
Show:
© 2014 Microsoft