Configurazione dei comportamenti clientConfiguring Client Behaviors

Windows Communication Foundation (WCF) consente di configurare i comportamenti in due modi: facendo riferimento alle configurazioni dei comportamenti sono definite nel <behavior> sezione di un file di configurazione dell'applicazione client- o a livello di chiamata applicazione.Windows Communication Foundation (WCF) configures behaviors in two ways: either by referring to behavior configurations -- which are defined in the <behavior> section of a client application configuration file – or programmatically in the calling application. In questo argomento vengono illustrati entrambi gli approcci.This topic describes both approaches.

Quando di usa un file di configurazione, la configurazione del comportamento è una raccolta denominata di impostazioni di configurazione.When using a configuration file, behavior configuration is a named collection of configuration settings. Il nome di ogni configurazione di comportamento deve essere univoco.The name of each behavior configuration must be unique. Questa stringa viene usata nell'attributo behaviorConfiguration di una configurazione endpoint per collegare l'endpoint al comportamento.This string is used in the behaviorConfiguration attribute of an endpoint configuration to link the endpoint to the behavior.

EsempioExample

Nel codice di configurazione seguente viene definito un comportamento denominato myBehavior.The following configuration code defines a behavior called myBehavior. L'endpoint client fa riferimento a questo comportamento nell'attributo behaviorConfiguration.The client endpoint references this behavior in the behaviorConfiguration attribute.

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

Uso di comportamenti a livello di programmazioneUsing Behaviors Programmatically

È anche possibile configurare o inserire comportamenti a livello di programmazione individuando appropriata Behaviors proprietà per l'oggetto client di Windows Communication Foundation (WCF) o per l'oggetto factory di canale client prima dell'apertura del client.You can also configure or insert behaviors programmatically by locating the appropriate Behaviors property on the Windows Communication Foundation (WCF) client object or on the client channel factory object prior to opening the client.

EsempioExample

Nell'esempio di codice seguente viene illustrato come inserire un comportamento a livello di programmazione accedendo alla proprietà Behaviors nell'oggetto ServiceEndpoint restituito dalla proprietà Endpoint prima della creazione dell'oggetto channel.The following code example shows how to programmatically insert a behavior by accessing the Behaviors property on the ServiceEndpoint returned from the Endpoint property prior to the creation of the channel object.

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

Vedere ancheSee Also

<i comportamenti ><behaviors>