Sdílet prostřednictvím


Konfigurace chování klientů

Windows Communication Foundation (WCF) konfiguruje chování dvěma způsoby: buď odkazem na konfigurace chování – které jsou definovány v <behavior> části konfiguračního souboru klientské aplikace – nebo programově ve volající aplikaci. Toto téma popisuje oba přístupy.

Při použití konfiguračního souboru je konfigurace chování pojmenovanou kolekcí nastavení konfigurace. Název každé konfigurace chování musí být jedinečný. Tento řetězec se používá v atributu behaviorConfiguration konfigurace koncového bodu k propojení koncového bodu s chováním.

Příklad 1

Následující konfigurační kód definuje chování s názvem myBehavior. Koncový bod klienta odkazuje na toto chování v atributu 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>  

Programové používání chování

Chování můžete také konfigurovat nebo vkládat programově tak, že před otevřením klienta vyhledejte odpovídající Behaviors vlastnost v objektu klienta Wcf (Windows Communication Foundation) nebo objektu objektu objektu pro vytváření kanálu klienta.

Příklad 2

Následující příklad kódu ukazuje, jak programově vložit chování přístupem k Behaviors vlastnosti na ServiceEndpoint vrácené z Endpoint vlastnosti před vytvořením objektu kanálu.

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

Viz také