方法 :.NET Framework クラスで自動トランザクションを使用する

自動トランザクションに参加させるようにクラスを設定する方法について説明します。

クラスを自動トランザクションに参加させるには

  1. ServicedComponent クラスからクラスを派生させ、そのクラスに TransactionAttribute を適用します。次の例では、TransactionAttribute attribute 属性を 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 を使用してキー ペアを作成し、AssemblyKeyFileAttribute または AssemblyKeyNameAttribute アセンブリ属性を追加して、厳密な名前でアセンブリに署名するためのキー ペアを格納するファイルの名前を指定します。

    <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

Copyright © 2007 by Microsoft Corporation.All rights reserved.