設定用戶端行為
Windows Communication Foundation (WCF) 會以兩種方式設定行為:一種是參考行為組態 (定義於用戶端應用程式組態檔的 <behavior>
區段中),另一種是以程式設計方式透過呼叫應用程式。 這個主題將描述這兩種方法。
使用組態檔時,行為組態都是具名的組態設定集合。 每個行為組態的名稱必須是獨一無二的。 會在端點組態的 behaviorConfiguration
屬性中使用這個字串,以便將端點連結至行為。
範例 1
下列組態程式碼會定義名稱為 myBehavior
的行為。 用戶端端點會參考 behaviorConfiguration
屬性中的這個行為。
<configuration>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="myBehavior">
<clientVia />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="myBinding" maxReceivedMessageSize="10000" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="myAddress" binding="basicHttpBinding" bindingConfiguration="myBinding" behaviorConfiguration="myBehavior" contract="myContract" />
</client>
</system.serviceModel>
</configuration>
以程式設計方式使用行為
您也可以透過程式設計方式來設定或插入行為,方法是在開啟用戶端之前,於 Windows Communication Foundation (WCF) 用戶端物件或用戶端通道處理站物件上尋找適當的 Behaviors
屬性。
範例 2
下列程式碼範例會示範如何以程式設計的方式插入行為,其方法是先存取傳回自 Behaviors 屬性之 ServiceEndpoint 上的 Endpoint 屬性,再建立通道物件。
public class Client
{
public static void Main()
{
try
{
// Picks up configuration from the config file.
ChannelFactory<ISampleServiceChannel> factory
= new ChannelFactory<ISampleServiceChannel>("WSHttpBinding_ISampleService");
// Add the client side behavior programmatically to all created channels.
factory.Endpoint.Behaviors.Add(new EndpointBehaviorMessageInspector());
ISampleServiceChannel wcfClientChannel = factory.CreateChannel();
// Making calls.
Console.WriteLine("Enter the greeting to send: ");
string greeting = Console.ReadLine();
Console.WriteLine("The service responded: " + wcfClientChannel.SampleMethod(greeting));
Console.WriteLine("Press ENTER to exit:");
Console.ReadLine();
// Done with service.
wcfClientChannel.Close();
Console.WriteLine("Done!");
}
catch (TimeoutException timeProblem)
{
Console.WriteLine("The service operation timed out. " + timeProblem.Message);
Console.Read();
}
catch (FaultException<SampleFault> fault)
{
Console.WriteLine("SampleFault fault occurred: {0}", fault.Detail.FaultMessage);
Console.Read();
}
catch (CommunicationException commProblem)
{
Console.WriteLine("There was a communication problem. " + commProblem.Message);
Console.Read();
}
}
Public Class Client
Public Shared Sub Main()
Try
' Picks up configuration from the config file.
Dim factory As New ChannelFactory(Of ISampleServiceChannel)("WSHttpBinding_ISampleService")
' Add the client side behavior programmatically to all created channels.
factory.Endpoint.Behaviors.Add(New EndpointBehaviorMessageInspector())
Dim wcfClientChannel As ISampleServiceChannel = factory.CreateChannel()
' Making calls.
Console.WriteLine("Enter the greeting to send: ")
Dim greeting As String = Console.ReadLine()
Console.WriteLine("The service responded: " & wcfClientChannel.SampleMethod(greeting))
Console.WriteLine("Press ENTER to exit:")
Console.ReadLine()
' Done with service.
wcfClientChannel.Close()
Console.WriteLine("Done!")
Catch timeProblem As TimeoutException
Console.WriteLine("The service operation timed out. " & timeProblem.Message)
Console.Read()
Catch fault As FaultException(Of SampleFault)
Console.WriteLine("SampleFault fault occurred: {0}", fault.Detail.FaultMessage)
Console.Read()
Catch commProblem As CommunicationException
Console.WriteLine("There was a communication problem. " & commProblem.Message)
Console.Read()
End Try
End Sub