Windows Communication Foundation (WCF) 用戶端,如同 Windows Communication Foundation (WCF) 服務,可以設定以調整執行時行為以符合用戶端應用程式的需求。 有三個屬性可用於指定用戶端執行時行為。 雙工客戶端回呼物件可使用CallbackBehaviorAttribute和CallbackDebugBehavior屬性來修改其執行階段行為。 另一個屬性 ClientViaBehavior可用來分隔邏輯目的地與實時網路目的地。 此外,雙工客戶端回呼型別可以使用某些服務端行為。 如需詳細資訊,請參閱 指定服務 Run-Time 行為。
使用 CallbackBehaviorAttribute
您可以使用 類別,在用戶端應用程式 CallbackBehaviorAttribute 中設定或擴充回呼合約實作的執行行為。 這個屬性對回呼類別執行與 ServiceBehaviorAttribute 類別類似的功能,但不包括實例化行為和交易設定。
類別 CallbackBehaviorAttribute 必須套用至實作回呼合約的類別。 如果將其應用於非雙工合約實作上,執行 InvalidOperationException 時會拋出例外。 下列程式代碼範例顯示 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
使用 CallbackDebugBehavior 啟用管理例外狀況資訊的傳遞
您可以在用戶端回呼物件中開啟受控例外狀況資訊的流程,以便進行偵錯,方法是從應用程式組態檔或程式設計中將 IncludeExceptionDetailInFaults 屬性設定為 true。
將 Managed 例外狀況資訊傳回至服務可能是安全性風險,因為例外狀況詳細數據會公開未經授權的服務可以使用的內部用戶端實作相關信息。 此外,雖然 CallbackDebugBehavior 屬性也可以以程式設計方式設定,但很容易忘記在部署時停用 IncludeExceptionDetailInFaults 。
由於涉及安全性問題,強烈建議:
您可以使用應用程式群組態檔將 屬性的值 IncludeExceptionDetailInFaults 設定為
true。您只能在受控制的偵錯案例中執行此動作。
下列程式代碼範例顯示用戶端組態檔,指示WCF 從SOAP訊息中的用戶端回呼物件傳回Managed 例外狀況資訊。
<client>
<endpoint
address="http://localhost:8080/DuplexHello"
binding="wsDualHttpBinding"
bindingConfiguration="WSDualHttpBinding_SampleDuplexHello"
contract="SampleDuplexHello"
name="WSDualHttpBinding_SampleDuplexHello"
behaviorConfiguration="enableCallbackDebug">
</endpoint>
</client>
<behaviors>
<endpointBehaviors>
<behavior name="enableCallbackDebug">
<callbackDebug includeExceptionDetailInFaults="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
使用 ClientViaBehavior 模式
您可以使用行為 ClientViaBehavior 來指定應該建立傳輸通道的統一資源識別碼。 當立即網路目的地不是訊息的預期處理器時,請使用此行為。 當呼叫應用程式不一定知道最終目的地或目的地 Via 標頭不是位址時,此功能可啟用多跳交談。