如何:在 .NET Framework 类中使用自动事务

下面的过程描述如何准备类以参与自动事务。

准备类以参与自动事务

  1. ServicedComponent 类派生您的类,并将 TransactionAttribute 应用于该类。以下示例说明了如何将 TransactionAttribute 属性应用于从 ServicedComponent 类派生的类。

    <Transaction(TransactionOption.Required)> Public Class Account
       Inherits ServicedComponent
       '. . .
    End Class
    
    [Transaction(TransactionOption.Required)]
    public class Account : ServicedComponent
    {
      //. . .
    }
    
  2. 对于不存在异常时必须为其自动调用 ContextUtil.SetComplete 方法的每个方法,应用 AutoComplete 属性。以下示例说明了如何应用 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.
    }
    
  3. 用强名称为程序集签名。若要使用属性为程序集签名,请使用 Sn.exe 创建一个密钥对,然后添加 AssemblyKeyFileAttributeAssemblyKeyNameAttribute 程序集属性,并指定包含该密钥对的文件的名称,以使用强名称为该程序集签名。

    <assembly: AssemblyKeyFileAttribute("TestApp.snk")>
    
    [assembly: AssemblyKeyFileAttribute("TestApp.snk")]
    
  4. 向 COM+ 目录注册包含此类的程序集。

  5. 如果调用类的实例的客户端是由公共语言运行库管理的,则注册将自动执行。但是,如果预期非托管调用方可能创建和调用此类的实例,请使用 .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;
  }
}

请参见

概念

自动事务中的投票

其他资源

编写服务组件

Footer image

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。