如何:使用 COM+ 的 BYOT(带来您自己的事务)功能
代码示例
下面的过程和示例说明从 ServicedComponent 类派生的类如何使用 COM+ 的 BYOT 功能来访问分布式事务处理协调器 (DTC)。有关从 System.EnterpriseServices 命名空间内使用 BYOT 功能的详细信息,请参阅 BYOT(带来您自己的事务)。
创建一个对象,该对象使用来自其他对象的事务。
定义从 ServicedComponent 类派生的类,并公开返回当前 COM+ DTC 事务对象的公共属性。例如,在下面的代码中,类
Base
继承 ServicedComponent 类,并且具有一个公共属性,该属性为当前 COM+ DTC 事务(ContextUtil.Transaction 对象在该事务中被实例化)返回 ContextUtil.Transaction 对象。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; } } }
创建两个从在步骤 1 中定义的类派生的类,并为这两个类设置事务属性。例如,下面的代码声明了两个从类
Base
派生的类,CTransaction1
和CTransaction2
,并设置了它们的事务属性。<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. }
创建一个使用在步骤 2 中创建的类的客户端应用程序,以便测试 BYOT 功能。
创建在步骤 2 中定义的其中一个类的实例。例如,下面的代码创建
CTransaction1
类的实例。Class BYOTDemonstration Public Shared Sub Main() Dim tx1 as New CTransaction1 End Sub End Class
class BYOTDemonstration { static void Main() { CTransaction1 tx1 = new CTransaction1(); } }
从在步骤 3.a 中创建的对象检索当前 COM+ DTC 事务。例如,下面的代码从
CTransaction1
类的公共属性Transaction
检索事务对象。Dim tx as Object = tx1.Transaction
Object tx = tx1.Transaction;
通过使用在步骤 3.b 中检索到的事务对象,创建在步骤 2 中定义的另一个类的实例。例如,下面的代码通过使用在步骤 3.b 中检索到的事务对象创建类
CTransaction2
的实例。Dim tx2 as CTransaction2 = ctype(BYOT.CreateWithTransaction(tx, _ gettype(CTransaction2)),CTransaction2)
CTransaction2 tx2 = (CTransaction2)BYOT.CreateWithTransaction(tx, typeof(CTransaction2));
生成强密钥并编译示例,如下所示:
sn –k BYOTDemo.snk vbc /r:System.EnterpriseServices.dll BYOTDemo.vb
sn –k BYOTDemo.snk
示例
Imports System
Imports System.Reflection
Imports System.EnterpriseServices
<assembly: AssemblyKeyFile("BYOTDemo.snk")>
Public Class Base
Inherits ServicedComponent
Public Readonly Property Transaction() as Object
Get
Return ContextUtil.Transaction
End Get
End Property
End Class
<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
Class BYOTDemonstration
Public Shared Sub Main()
' Create a transactional object, and then get its
' transaction.
Dim tx1 as New CTransaction1
Console.WriteLine("Created transaction1.")
Dim tx as Object = tx1.Transaction
Console.WriteLine("Got the transaction of transaction1.")
Dim tx2 as CTransaction2 = ctype(BYOT.CreateWithTransaction(tx, _ gettype(CTransaction2)),CTransaction2)
Console.WriteLine("Created transaction2 with the _
transaction of transaction1.")
End Sub
End Class
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.");
}
}
请参见
参考
System.EnterpriseServices Namespace
概念
版权所有 (C) 2007 Microsoft Corporation。保留所有权利。