How Do I...Use System.Transactions with EnterpriseServices Transactions?
System.Transactions provides functionality that allows interoperability with EnterpriseServices transactions. This sample will show how to use BYOT (Bring Your Own Transactions) with System.Transactions.
What is BYOT?
BYOT allows a component to be created with, or to inherit, an external transaction. That is, a component that does not already have an associated transaction can acquire a transaction. This is done by setting an arbitrary pre-existing transaction as the transaction property of a new component's context. In this case this is the transactions started with System.Transactions.
Convert the transaction:
Because it is necessary to use the BYOT functionality provided by the System.EnterpriseServices namespace, you must first convert the current transaction to be of the System.EnterpriseServices.Itransaction type. This can be done by the static TransactionInterop.GetDtcTransaction method as follows.
System.EnterpriseServices.ITransaction estx = null;
estx = (System.EnterpriseServices.ITransaction) TransactionInterop.GetDtcTransaction(Transaction.Current);
C#
Associate a component with the transaction:
You can use the CreateWithTransaction method of the System.EnterpriseServices.BYOT class to do the actual work. In this case, an object, SvcComp, inheriting from System.EnterpriseServices.ServicedComponent will receive a reference to the requested transaction.
SvcComp sc = (SvcComp) BYOT.CreateWithTransaction(estx, typeof(SvcComp));
C#
Obtain context information for the component:
Next, the System.EnterpriseServices.ContextUtil class is used to obtain information about the context of the object that inherits this transaction. Specifically, the sample must determine the GUID of the current transaction, and whether the current context is transactional. The following properties are used to obtain this information.
Console.WriteLine("IsInTransaction {0}", ContextUtil.IsInTransaction);
Console.WriteLine("TransactionId {0}", ContextUtil.TransactionId);
C#
Here is a full example:
[This sample can be found at M:\web\users\Sites\AspnetQuickStart\v2.0\QuickStart\howto\samples\Transactions\byot
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild
passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin
directory.]
Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.
|