OperationBehaviorAttribute.TransactionScopeRequired 속성

정의

메서드 실행에 트랜잭션 범위가 필요한지 여부를 나타내는 값을 가져오거나 설정합니다.

public:
 property bool TransactionScopeRequired { bool get(); void set(bool value); };
public bool TransactionScopeRequired { get; set; }
member this.TransactionScopeRequired : bool with get, set
Public Property TransactionScopeRequired As Boolean

속성 값

Boolean

메서드 실행에 트랜잭션 범위가 필요하면 true이고, 그렇지 않으면 false입니다. 기본값은 false입니다.

예제

다음 코드 예제에서는 필수 분산 트랜잭션 내에서 실행되는 작업을 보여 줍니다. TransactionScopeRequired 속성은 작업이 트랜잭션 범위에서 실행됨을 나타내며, TransactionAutoComplete 속성은 처리되지 않은 예외가 발생하지 않을 경우 트랜잭션 범위가 자동으로 완료됨을 나타냅니다. 처리되지 않은 예외가 발생하면 트랜잭션은 중단됩니다.

using System;
using System.ServiceModel;
using System.Transactions;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(Namespace="http://microsoft.wcf.documentation", SessionMode=SessionMode.Required)]
  public interface IBehaviorService
  {
    [OperationContract]
    string TxWork(string message);
  }

  // Note: To use the TransactionIsolationLevel property, you
  // must add a reference to the System.Transactions.dll assembly.
  /* The following service implementation:
   *   -- Processes messages on one thread at a time
   *   -- Creates one service object per session
   *   -- Releases the service object when the transaction commits
   */
  [ServiceBehavior(
    ConcurrencyMode=ConcurrencyMode.Single,
    InstanceContextMode=InstanceContextMode.PerSession,
    ReleaseServiceInstanceOnTransactionComplete=true
  )]
  public class BehaviorService : IBehaviorService, IDisposable
  {
    Guid myID;

    public BehaviorService()
    {
      myID = Guid.NewGuid();
      Console.WriteLine(
        "Object "
        + myID.ToString()
        + " created.");
    }

    /*
     * The following operation-level behaviors are specified:
     *   -- Always executes under a transaction scope.
     *   -- The transaction scope is completed when the operation terminates
     *       without an unhandled exception.
     */
    [OperationBehavior(
      TransactionAutoComplete = true,
      TransactionScopeRequired = true
    )]
    [TransactionFlow(TransactionFlowOption.Mandatory)]
    public string TxWork(string message)
    {
      // Do some transactable work.
      Console.WriteLine("TxWork called with: " + message);
      // Display transaction information.

      TransactionInformation info = Transaction.Current.TransactionInformation;
      Console.WriteLine("The distributed tx ID: {0}.", info.DistributedIdentifier);
      Console.WriteLine("The tx status: {0}.", info.Status);
      return String.Format("Hello. This was object {0}.",myID.ToString()) ;
    }

    public void Dispose()
    {
      Console.WriteLine(
        "Service "
        + myID.ToString()
        + " is being recycled."
      );
    }
  }
}
Imports System.ServiceModel
Imports System.Transactions

Namespace Microsoft.WCF.Documentation
  <ServiceContract(Namespace:="http://microsoft.wcf.documentation", SessionMode:=SessionMode.Required)> _
  Public Interface IBehaviorService
    <OperationContract> _
    Function TxWork(ByVal message As String) As String
  End Interface

    ' Note: To use the TransactionIsolationLevel property, you 
    ' must add a reference to the System.Transactions.dll assembly.
    ' The following service implementation:
    '   *   -- Processes messages on one thread at a time
    '   *   -- Creates one service object per session
    '   *   -- Releases the service object when the transaction commits

    <ServiceBehavior(ConcurrencyMode:=ConcurrencyMode.Single, InstanceContextMode:=InstanceContextMode.PerSession, _
                     ReleaseServiceInstanceOnTransactionComplete:=True)> _
    Public Class BehaviorService
        Implements IBehaviorService, IDisposable
        Private myID As Guid

        Public Sub New()
            myID = Guid.NewGuid()
            Console.WriteLine("Object " & myID.ToString() & " created.")
        End Sub

        '    
        '     * The following operation-level behaviors are specified:
        '     *   -- Always executes under a transaction scope.
        '     *   -- The transaction scope is completed when the operation terminates 
        '     *       without an unhandled exception.
        '     
        <OperationBehavior(TransactionAutoComplete:=True, TransactionScopeRequired:=True), _
        TransactionFlow(TransactionFlowOption.Mandatory)> _
        Public Function TxWork(ByVal message As String) As String Implements IBehaviorService.TxWork
            ' Do some transactable work.
            Console.WriteLine("TxWork called with: " & message)
            ' Display transaction information.

            Dim info As TransactionInformation = Transaction.Current.TransactionInformation
            Console.WriteLine("The distributed tx ID: {0}.", info.DistributedIdentifier)
            Console.WriteLine("The tx status: {0}.", info.Status)
            Return String.Format("Hello. This was object {0}.", myID.ToString())
        End Function

        Public Sub Dispose() Implements IDisposable.Dispose
            Console.WriteLine("Service " & myID.ToString() & " is being recycled.")
        End Sub
    End Class
End Namespace

설명

작업이 트랜잭션 범위 내에서 실행되도록 하려면 TransactionScopeRequiredtrue로 설정합니다. 흐름이 지정된 트랜잭션을 사용할 수 있는 경우 작업은 해당 트랜잭션 내에서 실행됩니다. 그렇지 않으면 새 트랜잭션이 만들어져 작업 실행에 사용됩니다. 엔드포인트에서 지정된 바인딩이 흐름이 지정된 트랜잭션 지원 여부를 제어합니다. 따라서 적절한 동작을 얻기 위해서는 바인딩에서 트랜잭션 흐름을 허용하는지 여부와 TransactionScopeRequired 속성 간의 상호 작용을 이해해야 합니다. 다음 표에는 가능한 동작이 나와 있습니다.

TransactionScopeRequired 바인딩이 트랜잭션 흐름 허용 호출자가 트랜잭션 흐름 지정 결과
False False 메서드가 트랜잭션 없이 실행됩니다.
True False 메서드가 새 트랜잭션 내에서 생성되고 실행됩니다.
True 또는 False False 트랜잭션 헤더에 대해 SOAP 오류가 반환됩니다.
False True 메서드가 트랜잭션 없이 실행됩니다.
True True 메서드가 흐름이 지정된 트랜잭션에서 실행됩니다.

적용 대상