ServiceBehaviorAttribute.ReleaseServiceInstanceOnTransactionComplete ServiceBehaviorAttribute.ReleaseServiceInstanceOnTransactionComplete ServiceBehaviorAttribute.ReleaseServiceInstanceOnTransactionComplete ServiceBehaviorAttribute.ReleaseServiceInstanceOnTransactionComplete Property

定義

現在のトランザクションの完了時にサービス オブジェクトを解放するかどうかを指定する値を取得または設定します。Gets or sets a value that specifies whether the service object is released when the current transaction completes.

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

プロパティ値

サービス オブジェクトが解放される場合は true。それ以外の場合は falsetrue if the service object is to be released; otherwise, false. 既定値は、true です。The default is true.

次のコード例では、トランザクションの分離レベルを ReadCommitted に設定し、同時トランザクションのサポートを無効にし、操作の呼び出しからのフロー トランザクションを要求し、未処理の例外が発生しなかった場合はサービスのトランザクションを自動的にコミットします。The following code example sets the transaction isolation level to ReadCommitted, disables support for concurrent transactions, requires a flowed transaction from the operation call, and commits the transaction on the service automatically if no unhandled exception occurs.

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
   *   -- Does not release the service object when the transaction commits
   *   -- The isolation level is read committed.
   */
  [ServiceBehavior(
    ConcurrencyMode=ConcurrencyMode.Single,
    InstanceContextMode=InstanceContextMode.PerSession,
    ReleaseServiceInstanceOnTransactionComplete=false,
    TransactionIsolationLevel=System.Transactions.IsolationLevel.ReadCommitted
  )]
  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
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
'   *   -- Does not release the service object when the transaction commits
'   *   -- The isolation level is read committed.
'   
    <ServiceBehavior(ConcurrencyMode:=ConcurrencyMode.Single, InstanceContextMode:=InstanceContextMode.PerSession, _
      ReleaseServiceInstanceOnTransactionComplete:=False, TransactionIsolationLevel:=System.Transactions.IsolationLevel.ReadCommitted)> _
    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

次のコード例が適切に動作するには、基になるバインドでフロー トランザクションがサポートされている必要があります。The underlying binding must support flowed transactions for the following code example to execute properly. たとえば、WSHttpBinding を使用するフロー トランザクションをサポートするには、コードまたはアプリケーション構成ファイル内で TransactionFlow プロパティを true に設定します。To support flowed transactions using the WSHttpBinding, for example, set the TransactionFlow property to true in code or in an application configuration file. 次のコード例は、前のサンプルの構成ファイルを示しています。The following code example shows a configuration file for the preceding sample.

<configuration>
  <system.serviceModel>
    <services>
      <service  
        name="Microsoft.WCF.Documentation.BehaviorService" 
        behaviorConfiguration="mex"
        >
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/BehaviorService"/>
            <add baseAddress="net.tcp://localhost:8081/BehaviorService"/>
          </baseAddresses>
        </host>
        <!--
          Note:
            This example code uses the WSHttpBinding to support transactions using the 
            WS-AtomicTransactions (WS-AT) protocol. WSHttpBinding is configured to use the  
            protocol, but the protocol is not enabled on some computers. Use the xws_reg -wsat+ 
            command to enable the WS-AtomicTransactions protocol in the MSDTC service.          
          -->
        <endpoint 
           contract="Microsoft.WCF.Documentation.IBehaviorService"
           binding="wsHttpBinding"
           bindingConfiguration="wsHttpBindingWithTXFlow"
           address="http://localhost:8080/BehaviorService"
          />
        <endpoint 
               contract="Microsoft.WCF.Documentation.IBehaviorService"
               binding="netTcpBinding"
               bindingConfiguration="netTcpBindingWithTXFlow"
               address="net.tcp://localhost:8081/BehaviorService"
          />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mex">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!-- binding configuration - configures a WSHttpBinding to require transaction flow -->
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpBindingWithTXFlow" transactionFlow="true" />
      </wsHttpBinding>
      <netTcpBinding>
        <binding name="netTcpBindingWithTXFlow" transactionFlow="true" />
      </netTcpBinding>
    </bindings>

  </system.serviceModel>
</configuration>

注釈

ReleaseServiceInstanceOnTransactionComplete が true に設定された操作があるときに falseOperationBehaviorAttribute.TransactionScopeRequired に設定する場合は、ConcurrencyModeReentrant に明示的に設定する必要があることに注意してください。Note that you must explicitly set ReleaseServiceInstanceOnTransactionComplete to false if there is an operation with OperationBehaviorAttribute.TransactionScopeRequired set to true and you set ConcurrencyMode to Reentrant. ReleaseServiceInstanceOnTransactionComplete の既定値は true であるため、この設定を行わなかった場合は検証例外がスローされます。Otherwise a validation exception is thrown because the default value of ReleaseServiceInstanceOnTransactionComplete is true.

さらに、サービスがサービス オブジェクトを ServiceHost.ServiceHost(Object, Uri[]) コンストラクターに渡すことで作成される場合、このプロパティの値は、false であるかのように処理されることを認識しておくことが重要です。In addition, it is important to realize that if the service is created by passing a service object to the ServiceHost.ServiceHost(Object, Uri[]) constructor, the value of this property is treated as if it were false.

適用対象