Share via


OperationBehaviorAttribute.TransactionScopeRequired Vlastnost

Definice

Získá nebo nastaví hodnotu, která označuje, zda metoda vyžaduje obor transakce pro její provedení.

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

Hodnota vlastnosti

truepokud metoda vyžaduje provedení oboru transakce; v opačném případě . false Výchozí formát je false.

Příklady

Následující příklad kódu ukazuje operaci, která se provádí v rámci povinné distribuované transakce. Vlastnost TransactionScopeRequired označuje, že se operace provádí v rámci oboru transakce, a TransactionAutoComplete vlastnost označuje, že pokud nedojde k žádné neošetřené výjimce, je obor transakce dokončen automaticky. Pokud dojde k neošetřené výjimce, transakce je přerušena.

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

Poznámky

Nastavte na TransactionScopeRequired , true pokud chcete, aby se vaše operace spustila v rámci oboru transakce. Pokud je k dispozici tok transakce, provede se operace v rámci této transakce. Pokud není k dispozici, vytvoří se nová transakce a použije se k provedení operace. Vazba zadaná v koncovém bodu řídí, jestli se podporují toky transakcí. Chcete-li tedy získat správné chování, musíte porozumět interakci mezi tím, zda je tok transakce povolen vazbou a TransactionScopeRequired vlastnost. Následující tabulka ukazuje možné chování.

Transactionscoperequired Vazba umožňuje tok transakce. Transakce toků volajícího Výsledek
Nepravda Nepravda No Metoda provede bez transakce.
Ano Ne No Metoda vytvoří a spustí v rámci nové transakce.
Pravda nebo Nepravda Ne Yes Pro hlavičku transakce se vrátí chyba SOAP.
Nepravda Ano Yes Metoda provede bez transakce.
Ano Ano Yes Metoda se spustí v rámci toku transakce.

Platí pro