방법: .NET Framework 클래스에서 자동 트랜잭션 사용

다음 절차에서는 자동 트랜잭션에 참여할 클래스를 준비하는 방법에 대해 설명합니다.

자동 트랜잭션에 참여할 클래스를 준비하려면

  1. ServicedComponent 클래스에서 클래스를 파생하고 해당 클래스에 TransactionAttribute를 적용합니다. 다음 예제에서는 ServicedComponent 클래스에서 파생된 클래스에 TransactionAttribute 특성을 적용하는 방법을 보여 줍니다.

    <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.