OperationBehaviorAttribute.TransactionScopeRequired Właściwość

Definicja

Pobiera lub ustawia wartość, która wskazuje, czy metoda wymaga zakresu transakcji dla jego wykonania.

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

Wartość właściwości

Boolean

true jeśli metoda wymaga wykonania zakresu transakcji; w przeciwnym razie , false. Wartość domyślna to false.

Przykłady

Poniższy przykład kodu przedstawia operację wykonywaną w ramach obowiązkowej transakcji rozproszonej. Właściwość TransactionScopeRequired wskazuje, że operacja jest wykonywana w zakresie transakcji, a TransactionAutoComplete właściwość wskazuje, że jeśli nie wystąpią nieobsługiwane wyjątki, zakres transakcji zostanie ukończony automatycznie. Jeśli wystąpi nieobsługiwany wyjątek, transakcja zostanie przerwana.

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

Uwagi

TransactionScopeRequired Ustaw wartość na , aby true wymagać wykonania operacji w zakresie transakcji. Jeśli przepływa transakcja jest dostępna, operacja jest wykonywana w ramach tej transakcji. Jeśli jedna z nich jest niedostępna, zostanie utworzona nowa transakcja i zostanie użyta do wykonania operacji. Powiązanie określone w punkcie końcowym określa, czy przepływane transakcje są obsługiwane. W związku z tym, aby uzyskać odpowiednie zachowanie, należy zrozumieć interakcję między tym, czy przepływ transakcji jest dozwolony przez powiązanie i TransactionScopeRequired właściwość. W poniższej tabeli przedstawiono możliwe zachowanie.

Transactionscoperequired Powiązanie zezwala na przepływ transakcji Transakcja przepływów wywołujących Wynik
Fałsz Fałsz Nie Metoda jest wykonywana bez transakcji.
Prawda Fałsz Nie Metoda tworzy i wykonuje w ramach nowej transakcji.
Prawda lub fałsz Fałsz Tak Błąd protokołu SOAP jest zwracany dla nagłówka transakcji.
Fałsz Prawda Tak Metoda jest wykonywana bez transakcji.
Prawda Prawda Tak Metoda jest wykonywana w ramach przepływanej transakcji.

Dotyczy