ServiceBehaviorAttribute クラス

定義

サービス コントラクトの実装の内部実行動作を指定します。

public ref class ServiceBehaviorAttribute sealed : Attribute, System::ServiceModel::Description::IServiceBehavior
[System.AttributeUsage(System.AttributeTargets.Class)]
public sealed class ServiceBehaviorAttribute : Attribute, System.ServiceModel.Description.IServiceBehavior
[<System.AttributeUsage(System.AttributeTargets.Class)>]
type ServiceBehaviorAttribute = class
    inherit Attribute
    interface IServiceBehavior
Public NotInheritable Class ServiceBehaviorAttribute
Inherits Attribute
Implements IServiceBehavior
継承
ServiceBehaviorAttribute
属性
実装

次のコード例は、ServiceBehaviorAttribute プロパティを示しています。 BehaviorService クラスは、次のことを示すために ServiceBehaviorAttribute 属性を使用します。

  • サービス オブジェクトは、トランザクションの完了時にリサイクルされます。

  • セッションごとに 1 つのサービス オブジェクトが存在します。

  • サービスはシングル スレッドであり、再入呼び出しをサポートしません。

さらに、OperationBehaviorAttribute 値は操作レベルで、TxWork メソッドがフロー トランザクションに自動的に登録するかまたはこの処理のための新しいトランザクションを作成するかどうか、および未処理の例外が発生しない場合にトランザクションが自動的にコミットされるかどうかを示します。

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:
     *   -- The executing transaction is committed when
     *        the operation completes without an
     *        unhandled exception
     *   -- Always executes under a flowed transaction.
     */
    [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:
        '     *   -- The executing transaction is committed when
        '     *        the operation completes without an 
        '     *        unhandled exception
        '     *   -- Always executes under a flowed transaction.
        '     
        <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

次のコード例が適切に動作するには、基になるバインドでフロー トランザクションがサポートされている必要があります。 たとえば、WSHttpBinding を使用するフロー トランザクションをサポートするには、コードまたはアプリケーション構成ファイル内で TransactionFlow プロパティを true に設定します。 次のコード例は、前のサンプルの構成ファイルを示しています。

<configuration>
  <system.serviceModel>
    <services>
      <service  
        name="Microsoft.WCF.Documentation.BehaviorService" 
        behaviorConfiguration="metadataAndDebugEnabled"
      >
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/SampleService"/>
          </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"
          />
        <endpoint
          address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange"
        />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataAndDebugEnabled">
          <serviceDebug
            includeExceptionDetailInFaults="true"
          />
          <serviceMetadata
            httpGetEnabled="true"
            httpGetUrl=""
          />
        </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>

注釈

ServiceBehaviorAttribute 属性をサービスの実装に適用して、サービス全体の実行動作を指定します (メソッド レベルで実行動作を指定するには、 属性を OperationBehaviorAttribute 使用します)。この属性は、サービス実装にのみ適用できます。 動作例については、「 サービス: 動作のサンプル」を参照してください。

ServiceBehaviorAttribute プロパティは、開発者が実装する必要がある一般的な機能を可能にする Windows Communication Foundation (WCF) プログラミング モデル機能です。 これらの動作とその他の動作の詳細については、「 サービス Run-Time 動作の指定」を参照してください。 次のプロパティの一部が設定する基になるランタイム プロパティの詳細については、「 ServiceHost とサービス モデル レイヤーの拡張」を参照してください。

  • AddressFilterMode プロパティは、ディスパッチャー システムが、要求を処理するエンドポイントを検索するために使用するフィルターの種類を指定します。

  • AutomaticSessionShutdown プロパティは、チャネルが閉じられ、残っているすべてのメッセージの処理をサービスが終了した時点でセッションを自動的に閉じます。

  • ConcurrencyMode プロパティは、内部スレッド モデルを制御して、再入可能またはマルチスレッド サービスのサポートを有効にします。

  • ConfigurationName プロパティは、構成ファイル内で name 要素の <service> 属性で使用する名前を宣言するために使用されます。

  • IgnoreExtensionDataObject プロパティは、メッセージの処理では必要がない余分なシリアル化情報をランタイムが無視できるようにします。

  • IncludeExceptionDetailInFaults プロパティは、サービス内の未処理の例外を SOAP エラーとして返すかどうかを指定します。 これは、デバッグのみを目的としています。

  • InstanceContextMode プロパティは、クライアントとのメッセージ交換時にサービスとサービス オブジェクトをリサイクルするかどうかを指定します。また、リサイクルする場合は、どの時点でリサイクルするかを指定します。

  • MaxItemsInObjectGraph プロパティは、シリアル化されるオブジェクト グラフ内の項目数を制限します。

  • Name プロパティと Namespace プロパティは、サービス要素の WSDL 表現の名前と名前空間を制御します。

  • ReleaseServiceInstanceOnTransactionComplete プロパティは、トランザクションの完了時にサービス オブジェクトをリサイクルするかどうかを指定します。

  • TransactionAutoCompleteOnSessionClose プロパティは、セッションの終了時に未解決のトランザクションを完了するかどうかを指定します。

  • TransactionIsolationLevel プロパティは、コントラクトがサポートするトランザクションの分離レベルを指定します。

  • TransactionTimeout プロパティは、トランザクションを完了しなければならない期間を指定します。この期間内に完了しない場合、トランザクションは中止されます。

  • UseSynchronizationContext プロパティは、受信したメソッド呼び出しをユーザー インターフェイス スレッドと自動的に同期するかどうかを示します。

  • ValidateMustUnderstand プロパティは、MustUnderstand としてマークされた SOAP ヘッダーが間違いなく理解されたことを確認する必要があるかどうかをシステムに通知します。

IncludeExceptionDetailInFaults プロパティは、アプリケーション構成ファイルを使用して設定することもできます。 詳細については、「IncludeExceptionDetailInFaults」を参照してください。

コンストラクター

ServiceBehaviorAttribute()

ServiceBehaviorAttribute クラスの新しいインスタンスを初期化します。

プロパティ

AddressFilterMode

ディスパッチャーが受信メッセージを適切なエンドポイントにルーティングするために使用する AddressFilterMode を取得または設定します。

AutomaticSessionShutdown

クライアントが出力セッションを閉じたときにセッションを自動的に閉じるかどうかを指定します。

ConcurrencyMode

サービスが、1 つのスレッド、複数のスレッド、または再入呼び出しをサポートするかどうかを示す値を取得または設定します。

ConfigurationName

アプリケーション構成ファイル内でサービス要素を検索するために使用される値を取得または設定します。

EnsureOrderedDispatch

サービスの順番どおりのディスパッチが保証されるかどうかを示す値を取得または設定します。

IgnoreExtensionDataObject

不明なシリアル化データをネットワークで送信するかどうかを指定する値を取得または設定します。

IncludeExceptionDetailInFaults

一般的な未処理の実行例外を FaultException<TDetail> 型の ExceptionDetail に変換してエラー メッセージとして送信するように指定する値を取得または設定します。 この属性は、開発時にサービスのトラブルシューティングを行う場合にのみ、true に設定します。

InstanceContextMode

新しいサービス オブジェクトが作成されるかどうかを示す値を取得または設定します。

MaxItemsInObjectGraph

シリアル化されるオブジェクトで許可される項目の最大数を取得または設定します。

Name

Web サービス記述言語 (WSDL) でのサービス要素の名前属性の値を取得または設定します。

Namespace

Web サービス記述言語 (WSDL) でのサービスのターゲット名前属性の値を取得または設定します。

ReleaseServiceInstanceOnTransactionComplete

現在のトランザクションの完了時にサービス オブジェクトを解放するかどうかを指定する値を取得または設定します。

TransactionAutoCompleteOnSessionClose

現在のセッションがエラーなしで閉じたときに、保留中のトランザクションを完了するかどうかを指定する値を取得または設定します。

TransactionIsolationLevel

サービス内で作成される新しいトランザクションと、クライアントからフローされる受信トランザクションの分離レベルを指定します。

TransactionTimeout

トランザクションを完了する必要がある制限時間を取得または設定します。

TypeId

派生クラスで実装されると、この Attribute の一意の識別子を取得します。

(継承元 Attribute)
UseSynchronizationContext

現在の同期コンテキストを使用して実行スレッドを選択するかどうかを指定する値を取得または設定します。

ValidateMustUnderstand

システムまたはアプリケーションで SOAP MustUnderstand ヘッダー処理を強制的に行うかどうかを指定する値を取得または設定します。

メソッド

Equals(Object)

このインスタンスが、指定されたオブジェクトと等価であるかどうかを示す値を返します。

(継承元 Attribute)
GetHashCode()

このインスタンスのハッシュ コードを返します。

(継承元 Attribute)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
GetWellKnownSingleton()

サービスを実装し、サービスのシングルトン インスタンスとして使用されるオブジェクトを取得します。シングルトン インスタンスがない場合は null を取得します。

IsDefaultAttribute()

派生クラスでオーバーライドされるとき、このインスタンスの値が派生クラスの既定値であるかどうかを示します。

(継承元 Attribute)
Match(Object)

派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。

(継承元 Attribute)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
SetWellKnownSingleton(Object)

サービスを実装し、サービスのシングルトン インスタンスとして使用されるオブジェクトを指定します。

ShouldSerializeConfigurationName()

ConfigurationName プロパティが既定値から変更されたためにシリアル化する必要があるかどうかを示す値を返します。

ShouldSerializeReleaseServiceInstanceOnTransactionComplete()

ReleaseServiceInstanceOnTransactionComplete プロパティが既定値から変更されたためにシリアル化する必要があるかどうかを示す値を返します。

ShouldSerializeTransactionAutoCompleteOnSessionClose()

TransactionAutoCompleteOnSessionClose プロパティが既定値から変更されたためにシリアル化する必要があるかどうかを示す値を返します。

ShouldSerializeTransactionIsolationLevel()

TransactionIsolationLevel プロパティが既定値から変更されたためにシリアル化する必要があるかどうかを示す値を返します。

ShouldSerializeTransactionTimeout()

TransactionTimeout プロパティが既定値から変更されたためにシリアル化する必要があるかどうかを示す値を返します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。

(継承元 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。

(継承元 Attribute)
IServiceBehavior.AddBindingParameters(ServiceDescription, ServiceHostBase, Collection<ServiceEndpoint>, BindingParameterCollection)

動作プロパティをサポートするバインディングにカスタム データ オブジェクトを渡します。

IServiceBehavior.ApplyDispatchBehavior(ServiceDescription, ServiceHostBase)

動作プロパティをサポートするようにサービス ランタイムをカスタマイズします。

IServiceBehavior.Validate(ServiceDescription, ServiceHostBase)

サービスの説明とサービス ホストが動作をサポートできることを確認します。

適用対象

こちらもご覧ください