CallbackBehaviorAttribute 類別

定義

設定在用戶端應用程式中的回呼服務實作。

public ref class CallbackBehaviorAttribute sealed : Attribute, System::ServiceModel::Description::IEndpointBehavior
[System.AttributeUsage(System.AttributeTargets.Class)]
public sealed class CallbackBehaviorAttribute : Attribute, System.ServiceModel.Description.IEndpointBehavior
[<System.AttributeUsage(System.AttributeTargets.Class)>]
type CallbackBehaviorAttribute = class
    inherit Attribute
    interface IEndpointBehavior
Public NotInheritable Class CallbackBehaviorAttribute
Inherits Attribute
Implements IEndpointBehavior
繼承
CallbackBehaviorAttribute
屬性
實作

範例

下列程式碼範例會顯示回呼物件 (它使用 CallbackBehaviorAttribute 物件判斷要封送處理的執行緒) 的 SynchronizationContext、可執行訊息驗證的 ValidateMustUnderstand 屬性,以及可將例外狀況當做 IncludeExceptionDetailInFaults 物件傳回服務以進行偵錯的 FaultException 屬性。

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Threading;

namespace Microsoft.WCF.Documentation
{
  [CallbackBehaviorAttribute(
   IncludeExceptionDetailInFaults= true,
    UseSynchronizationContext=true,
    ValidateMustUnderstand=true
  )]
  public class Client : SampleDuplexHelloCallback
  {
    AutoResetEvent waitHandle;

    public Client()
    {
      waitHandle = new AutoResetEvent(false);
    }

    public void Run()
    {
      // Picks up configuration from the configuration file.
      SampleDuplexHelloClient wcfClient
        = new SampleDuplexHelloClient(new InstanceContext(this), "WSDualHttpBinding_SampleDuplexHello");
      try
      {
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Enter a greeting to send and press ENTER: ");
        Console.Write(">>> ");
        Console.ForegroundColor = ConsoleColor.Green;
        string greeting = Console.ReadLine();
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Called service with: \r\n\t" + greeting);
        wcfClient.Hello(greeting);
        Console.WriteLine("Execution passes service call and moves to the WaitHandle.");
        this.waitHandle.WaitOne();
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("Set was called.");
        Console.Write("Press ");
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write("ENTER");
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.Write(" to exit...");
        Console.ReadLine();
      }
      catch (TimeoutException timeProblem)
      {
        Console.WriteLine("The service operation timed out. " + timeProblem.Message);
        Console.ReadLine();
      }
      catch (CommunicationException commProblem)
      {
        Console.WriteLine("There was a communication problem. " + commProblem.Message);
        Console.ReadLine();
      }
    }
    public static void Main()
    {
      Client client = new Client();
      client.Run();
    }

    public void Reply(string response)
    {
      Console.WriteLine("Received output.");
      Console.WriteLine("\r\n\t" + response);
      this.waitHandle.Set();
    }
  }
}

Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Threading

Namespace Microsoft.WCF.Documentation
  <CallbackBehaviorAttribute(IncludeExceptionDetailInFaults:= True, UseSynchronizationContext:=True, ValidateMustUnderstand:=True)> _
  Public Class Client
      Implements SampleDuplexHelloCallback
    Private waitHandle As AutoResetEvent

    Public Sub New()
      waitHandle = New AutoResetEvent(False)
    End Sub

    Public Sub Run()
      ' Picks up configuration from the configuration file.
      Dim wcfClient As New SampleDuplexHelloClient(New InstanceContext(Me), "WSDualHttpBinding_SampleDuplexHello")
      Try
        Console.ForegroundColor = ConsoleColor.White
        Console.WriteLine("Enter a greeting to send and press ENTER: ")
        Console.Write(">>> ")
        Console.ForegroundColor = ConsoleColor.Green
        Dim greeting As String = Console.ReadLine()
        Console.ForegroundColor = ConsoleColor.White
        Console.WriteLine("Called service with: " & Constants.vbCrLf & Constants.vbTab & greeting)
        wcfClient.Hello(greeting)
        Console.WriteLine("Execution passes service call and moves to the WaitHandle.")
        Me.waitHandle.WaitOne()
        Console.ForegroundColor = ConsoleColor.Blue
        Console.WriteLine("Set was called.")
        Console.Write("Press ")
        Console.ForegroundColor = ConsoleColor.Red
        Console.Write("ENTER")
        Console.ForegroundColor = ConsoleColor.Blue
        Console.Write(" to exit...")
        Console.ReadLine()
      Catch timeProblem As TimeoutException
        Console.WriteLine("The service operation timed out. " & timeProblem.Message)
        Console.ReadLine()
      Catch commProblem As CommunicationException
        Console.WriteLine("There was a communication problem. " & commProblem.Message)
        Console.ReadLine()
      End Try
    End Sub
    Public Shared Sub Main()
      Dim client As New Client()
      client.Run()
    End Sub

    Public Sub Reply(ByVal response As String) Implements SampleDuplexHelloCallback.Reply
      Console.WriteLine("Received output.")
      Console.WriteLine(Constants.vbCrLf & Constants.vbTab & response)
      Me.waitHandle.Set()
    End Sub
  End Class
End Namespace

備註

使用 CallbackBehaviorAttribute 屬性,即可設定或擴充用戶端應用程式中回呼合約實作的執行行為。 這個屬性對回呼類別執行的函式相同於 ServiceBehaviorAttribute 屬性,而執行個體化行為和異動設定則為其例外。

CallbackBehaviorAttribute 必須套用至實作回呼合約的類別。 如果套用至非雙工合約實作,便會在執行階段擲回 InvalidOperationException 例外狀況。

注意

您也可以對回呼作業實作使用 OperationBehaviorAttribute 屬性。 然而,如果 OperationBehaviorAttribute 是用在回呼作業上,ReleaseInstanceMode 屬性必須是 None,否則會在執行階段擲回 InvalidOperationException 例外狀況。

同時會提供下列屬性:

建構函式

CallbackBehaviorAttribute()

初始化 CallbackBehaviorAttribute 類別的新執行個體。

屬性

AutomaticSessionShutdown

指定是否要在服務關閉雙工工作階段時自動關閉工作階段。

ConcurrencyMode

取得或設定服務是支援單一執行緒、多重執行緒或可重新進入的呼叫。

IgnoreExtensionDataObject

取得或設定值,這個值會指定是否要將未知的序列化資料傳送到網路上。

IncludeExceptionDetailInFaults

取得或設定值,指定一般未處理的執行例外狀況 (要轉換為型別 FaultException<TDetail>String),並傳送為錯誤訊息。 請只在開發期間將這個項目設定為 true,以針對服務進行疑難排解。

MaxItemsInObjectGraph

取得或設定已序列化之物件中允許的項目數目上限。

TransactionIsolationLevel

指定交易隔離等級。

TransactionTimeout

取得或設定異動必須完成的期間。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)
UseSynchronizationContext

取得或設定值,這個值會指定是否使用目前的同步處理內容來選擇執行的執行緒。

ValidateMustUnderstand

取得或設定值,這個值會指定系統或應用程式是否會強制執行 SOAP MustUnderstand 標頭處理。

方法

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)
IEndpointBehavior.AddBindingParameters(ServiceEndpoint, BindingParameterCollection)

設定繫結項目以支援回呼行為。

IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint, ClientRuntime)

設定用戶端執行階段以支援回呼物件。

IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint, EndpointDispatcher)

實作 ApplyDispatchBehavior(ServiceEndpoint, EndpointDispatcher) 方法。 這個實作沒有任何作用。

IEndpointBehavior.Validate(ServiceEndpoint)

在建置執行階段之前驗證端點描述。

適用於