Megosztás a következőn keresztül:


Ügyfél viselkedésének konfigurálása

A Windows Communication Foundation (WCF) kétféleképpen konfigurálja a viselkedéseket: vagy az ügyfélalkalmazás konfigurációs fájljának szakaszában <behavior> definiált viselkedéskonfigurációkra hivatkozva, vagy programozott módon a hívó alkalmazásban. Ez a témakör mindkét megközelítést ismerteti.

Konfigurációs fájl használatakor a viselkedéskonfiguráció a konfigurációs beállítások nevesített gyűjteménye. Az egyes viselkedéskonfigurációk nevének egyedinek kell lennie. Ez a behaviorConfiguration sztring egy végpontkonfiguráció attribútumában használatos a végpont viselkedéséhez való csatolásához.

1. példa

Az alábbi konfigurációs kód egy úgynevezett myBehaviorviselkedést határoz meg. Az ügyfélvégpont erre a viselkedésre hivatkozik az behaviorConfiguration attribútumban.

<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>  

Viselkedések programozott használata

A viselkedéseket programozott módon is konfigurálhatja vagy beszúrhatja a Behaviors Windows Communication Foundation (WCF) ügyfélobjektum megfelelő tulajdonságának megkeresésével vagy az ügyfélcsatorna gyári objektumán az ügyfél megnyitása előtt.

2. példa

Az alábbi példakód bemutatja, hogyan szúrhat be programozott módon viselkedést úgy, hogy a Behaviors csatornaobjektum létrehozása előtt hozzáfér a ServiceEndpoint Endpoint tulajdonságon visszaadott tulajdonsághoz.

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: {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

Lásd még