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 屬性會指定服務作業屬於可以、必須或無法模擬呼叫者的身分識別。

  • 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)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 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)

實作驗證行為。

適用於