OperationBehaviorAttribute クラス

定義

サービス メソッドのローカル実行動作を指定します。

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

必須の分散トランザクション内で実行する操作のコード例を次に示します。 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

注釈

OperationBehaviorAttribute 属性を使用して、操作が実行されるときの操作固有の実行動作を示します (サービス レベルの実行動作を指定するには、ServiceBehaviorAttribute 属性を使用します)。

注意

OperationBehaviorAttribute を使用して、双方向クライアント アプリケーションでコールバック コントラクト操作を設定することもできます。 コールバック操作で使用する場合は、ReleaseInstanceMode プロパティが None である必要があります。そうでない場合は、InvalidOperationException 例外が実行時にスローされます。

このOperationBehaviorAttribute属性は、開発者が独自に実装する必要がある一般的な機能を可能にする、Windows Communication Foundation (WCF) プログラミング モデル機能です。

  • AutoDisposeParameters プロパティは、操作に渡されたパラメーター オブジェクトを操作の完了時に破棄するかどうかを制御します。

  • TransactionAutoComplete プロパティは、未処理の例外が発生しなかった場合に、メソッドが実行されているトランザクションが自動的にコミットされるかどうかを指定します。

  • TransactionScopeRequired プロパティは、メソッドをトランザクションの中で実行する必要があるかどうかを指定します。

  • Impersonation プロパティは、サービス操作が、呼び出し元の ID を偽装できるか、偽装する必要があるか、または偽装できないかを指定します。

  • ReleaseInstanceMode プロパティは、サービス オブジェクトが、メソッドの呼び出しプロセスの間にリサイクルされるかどうかを指定します。

コンストラクター

OperationBehaviorAttribute()

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

プロパティ

AutoDisposeParameters

パラメーターを自動的に破棄するかどうかを示す値を取得または設定します。

Impersonation

操作がサポートする呼び出し元の偽装レベルを示す値を取得または設定します。

ReleaseInstanceMode

操作呼び出しのどの時点でサービス オブジェクトをリサイクルするかを示す値を取得または設定します。

TransactionAutoComplete

未処理の例外が発生しなかった場合は、現在のトランザクション スコープを自動的にコミットするかどうかを示す値を取得または設定します。

TransactionScopeRequired

メソッドが、実行のためにトランザクション スコープを必要とするかどうかを示す値を取得または設定します。

TypeId

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

(継承元 Attribute)

メソッド

Equals(Object)

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

(継承元 Attribute)
GetHashCode()

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

(継承元 Attribute)
GetType()

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

(継承元 Object)
IsDefaultAttribute()

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

(継承元 Attribute)
Match(Object)

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

(継承元 Attribute)
MemberwiseClone()

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

(継承元 Object)
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)
IOperationBehavior.AddBindingParameters(OperationDescription, BindingParameterCollection)

AddBindingParameters(OperationDescription, BindingParameterCollection) メソッドを実装します。

IOperationBehavior.ApplyClientBehavior(OperationDescription, ClientOperation)

クライアントのオペレーション動作を実装します。

IOperationBehavior.ApplyDispatchBehavior(OperationDescription, DispatchOperation)

サービス操作の動作を実装します。

IOperationBehavior.Validate(OperationDescription)

検証動作を実装します。

適用対象