Udostępnij przez


Konfigurowanie zachowań klienta

Program Windows Communication Foundation (WCF) konfiguruje zachowania na dwa sposoby: odwołując się do konfiguracji zachowania — zdefiniowanych w <behavior> sekcji pliku konfiguracji aplikacji klienckiej — lub programowo w aplikacji wywołującej. W tym temacie opisano oba podejścia.

W przypadku korzystania z pliku konfiguracji konfiguracja konfiguracji jest nazwą kolekcji ustawień konfiguracji. Nazwa każdej konfiguracji zachowania musi być unikatowa. Ten ciąg jest używany w atrybucie behaviorConfiguration konfiguracji punktu końcowego w celu połączenia punktu końcowego z zachowaniem.

Przykład 1

Poniższy kod konfiguracji definiuje zachowanie o nazwie myBehavior. Punkt końcowy klienta odwołuje się do tego zachowania w atrybucie 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>  

Programowe używanie zachowań

Można również programowo skonfigurować lub wstawić zachowania, lokalizując odpowiednią Behaviors właściwość w obiekcie klienta programu Windows Communication Foundation (WCF) lub w obiekcie fabryki kanału klienta przed otwarciem klienta.

Przykład 2

Poniższy przykład kodu pokazuje, jak programowo wstawić zachowanie przez uzyskanie Behaviors dostępu do właściwości zwróconej z Endpoint właściwości ServiceEndpoint przed utworzeniem obiektu kanału.

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

Zobacz też