HOW TO:使用 COM+ 的 BYOT (Bring Your Own Transaction) 功能
程式碼範例
以下程序和範例示範衍生自 ServicedComponent 類別的類別如何使用 COM+ 的 BYOT 功能,存取分散式交易協調器 (DTC)。如需從 System.EnterpriseServices 命名空間內部使用 BYOT 功能的詳細資訊,請參閱 BYOT (Bring Your Own Transaction)。
建立使用來自另一個物件之交易的物件。
定義一個衍生自 ServicedComponent 類別的類別,並公開可傳回目前 COM+ DTC 交易物件的公開屬性。例如下列程式碼中,類別
Base
繼承了 ServicedComponent 類別,並具有可為目前 COM+ DTC 交易物件 (物件從此處產生) 傳回 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 定義之類別所衍生的類別,並為這兩個類別設定 Transaction 屬性。例如,以下程式碼宣告兩個類別
CTransaction1
和CTransaction2
(衍生自類別Base
),並分別設定其 Transaction 屬性。<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
擷取 Transaction 物件。Dim tx as Object = tx1.Transaction
Object tx = tx1.Transaction;
為步驟 2 定義的第二個類別建立執行個體,方法為使用步驟 3.b 所擷取的 Transaction 物件。例如,以下程式碼會使用步驟 3.b 所擷取的 Transaction,建立類別
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
概念
BYOT (Bring Your Own Transaction)
可用 COM+ 服務摘要
Copyright © 2007 by Microsoft Corporation. All rights reserved.