title | description | ms.date | dev_langs | ms.assetid | ||
---|---|---|---|---|---|---|
Configuring Client Behaviors |
Learn about the two ways that WCF configures behaviors: in the application configuration file or programmatically from the calling application. |
03/30/2017 |
|
df5b32fa-e73b-4e8e-b66f-357c748e0173 |
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. This topic describes both approaches.
When using a configuration file, behavior configuration is a named collection of configuration settings. The name of each behavior configuration must be unique. This string is used in the behaviorConfiguration
attribute of an endpoint configuration to link the endpoint to the behavior.
The following configuration code defines a behavior called myBehavior
. 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>
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.
The following code example shows how to programmatically insert a behavior by accessing the xref:System.ServiceModel.Description.ServiceEndpoint.Behaviors%2A property on the xref:System.ServiceModel.Description.ServiceEndpoint returned from the xref:System.ServiceModel.ChannelFactory.Endpoint%2A property prior to the creation of the channel object.
[!code-csharpChannelFactoryBehaviors#10] [!code-vbChannelFactoryBehaviors#10]