以下程序描述如何準備類別以參與自動交易。
準備類別以參與自動交易
從 ServicedComponent 類別衍生您的類別,然後將 TransactionAttribute 套用至此類別。以下範例示範如何將 TransactionAttribute 屬性套用至衍生自 ServicedComponent 類別的類別。
<Transaction(TransactionOption.Required)> Public Class Account Inherits ServicedComponent '. . . End Class[Transaction(TransactionOption.Required)] public class Account : ServicedComponent { //. . . }將 AutoComplete 屬性套用到每個方法,這些方法在沒有例外狀況下,必須自動呼叫 ContextUtil.SetComplete 方法。以下範例示範如何套用 AutoComplete 屬性:
<AutoComplete()> Public Sub Debit(amount As Integer) ' Do some database work. Any exception thrown here aborts the ' transaction; otherwise, transaction commits. End Sub[AutoComplete] public void Debit(int amount) { // Do some database work. Any exception thrown here aborts the // transaction; otherwise, transaction commits. }使用強式名稱簽署組件。若要使用屬性簽署組件,請使用 Sn.exe 建立金鑰組,然後新增 AssemblyKeyFileAttribute 或 AssemblyKeyNameAttribute 組件屬性,並指定包含金鑰組 (用來以強式名稱簽署組件) 的檔案名稱。
<assembly: AssemblyKeyFileAttribute("TestApp.snk")>[assembly: AssemblyKeyFileAttribute("TestApp.snk")]將內含類別的組件向 COM+ 目錄 (Catalog) 註冊。
如果呼叫類別執行個體的用戶端是由 Common Language Runtime 管理,便會為您執行註冊。然而,如果您預期 Unmanaged 呼叫者可能建立及呼叫類別的執行個體,請使用 .NET 服務安裝工具 (Regsvcs.exe) 手動執行註冊。
範例
' -----------------------------------------------------------------
' TestApp.vb
' Generate a Strong name:
' sn -k TestApp.snk
' Compile the code:
' vbc /target:exe /r:System.EnterpriseServices.dll TestApp.vb
' Run TestApp:
' start TestApp.exe
' -----------------------------------------------------------------
Option Explicit
Option Strict
Imports System
Imports System.Runtime.CompilerServices
Imports System.EnterpriseServices
Imports System.Reflection
'Registration details.
'COM+ application name as it appears in the COM+ catalog.
<assembly: ApplicationName("TestApp")>
'Strong name for assembly.
<assembly: AssemblyKeyFileAttribute("TestApp.snk")>
<Transaction(TransactionOption.Required)> Public Class Account
Inherits ServicedComponent
'Provides SetComplete behavior in the absence of exceptions.
<AutoComplete()> Public Sub Debit(amount As Integer)
' Do some database work. Any exception thrown here aborts the
' transaction; otherwise, transaction commits.
End Sub
End Class
Public Class client
Public Shared Sub Main()
Dim accountX As New Account()
accountX.Debit(100)
Environment.Exit(0)
End Sub
End Class
// -----------------------------------------------------------------
// TestApp.cs
// Generate a Strong name:
// sn -k TestApp.snk
// Compile the code:
// csc /target:exe /r:System.EnterpriseServices.dll TestApp.cs
// Run TestApp:
// start TestApp.exe
// -----------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
using System.EnterpriseServices;
using System.Reflection;
//Registration details.
//COM+ application name as it appears in the COM+ catalog.
[assembly: ApplicationName("TestApp")]
//Strong name for assembly.
[assembly: AssemblyKeyFileAttribute("TestApp.snk")]
[Transaction(TransactionOption.Required)]
public class Account : ServicedComponent
{
//Provides SetComplete behavior in the absence of exceptions.
[AutoComplete]
public void Debit(int amount)
{
// Do some database work. Any exception thrown here aborts the
// transaction; otherwise, transaction commits.
}
}
public class client
{
public static int Main()
{
Account accountX = new Account();
accountX.Debit(100);
return 0;
}
}
請參閱
概念
其他資源
.gif)
Copyright © 2007 by Microsoft Corporation. All rights reserved.