CallbackBehaviorAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在用戶端應用程式中設定回呼服務實作。
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 一個回調物件,該物件使用 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 執行時會拋出例外。
下列屬性可供使用:
當通道關閉且回調完成剩餘訊息處理時,該 AutomaticSessionShutdown 屬性會自動關閉會話。
此 ConcurrencyMode 特性控制內部執行緒模型,支援重入式或多執行緒回調物件。
此 IgnoreExtensionDataObject 特性使執行時能忽略處理訊息所需的額外序列化資訊。
此 IncludeExceptionDetailInFaults 特性指定服務中未處理的例外是否會以 SOAP 錯誤形式回傳至服務以供除錯。
該 MaxItemsInObjectGraph 性質限制了物件圖中序列化項目的數量。
屬性 TransactionIsolationLevel 指定了合約所支援的交易隔離層級。
該 TransactionTimeout 屬性指定交易必須在期限內完成或終止。
屬性 UseSynchronizationContext 表示是否要用目前 SynchronizationContext 物件自動同步入站的方法呼叫。
該 ValidateMustUnderstand 屬性會告知系統是否應該確認標記為
MustUnderstand的 SOAP 標頭確實已被理解。
建構函式
| 名稱 | Description |
|---|---|
| CallbackBehaviorAttribute() |
初始化 CallbackBehaviorAttribute 類別的新執行個體。 |
屬性
| 名稱 | Description |
|---|---|
| AutomaticSessionShutdown |
規定當服務關閉雙工會話時,是否自動關閉會話。 |
| ConcurrencyMode |
接收或設定服務是否支援單一執行緒、多個執行緒,或是重入呼叫。 |
| IgnoreExtensionDataObject |
取得或設定一個值,指定是否傳送未知序列化資料到線路上。 |
| IncludeExceptionDetailInFaults |
取得或設定一個值,指定一般未處理的執行異常要轉換成 FaultException<TDetail> 類型 String 並以錯誤訊息形式傳送。 設定 |
| MaxItemsInObjectGraph |
取得或設定序列化物件中允許的最大項目數量。 |
| TransactionIsolationLevel |
指定交易隔離等級。 |
| TransactionTimeout |
取得或設定交易必須完成的期限。 |
| TypeId |
在衍生類別中實作時,取得這個 Attribute的唯一標識碼。 (繼承來源 Attribute) |
| UseSynchronizationContext |
取得或設定一個值,指定是否使用目前的同步上下文來選擇執行執行緒。 |
| ValidateMustUnderstand |
取得或設定一個值,指定系統或應用程式是否強制執行 SOAP |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
傳回值,這個值表示這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| GetHashCode() |
傳回這個實例的哈希碼。 (繼承來源 Attribute) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| IsDefaultAttribute() |
在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。 (繼承來源 Attribute) |
| Match(Object) |
在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |
明確介面實作
| 名稱 | Description |
|---|---|
| _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) |
在建置執行時前驗證端點描述。 |