Configuring Behaviors


For behaviors to be used, they must be configured for use on the service or proxy. There are three possibilities for configuring behaviors: with attributes, in code, or via a configuration file.

Configuring Behaviors with Attributes

When an interface with attributes is first added to either a ChannelFactory or a Service Host, the Windows Communication Foundation identifies the set of attributes on the interface using reflection.

The following text gives an example of how to create an attribute and then an example of that attribute being applied.

The following code creates an attribute named Audit.

[AttributeUsage(AttributeTargets.Interface)] public class AuditAttribute: Attribute, IChannelBehavior {     public void ApplyBehavior(                               ChannelDescription description,                               ProxyBehavior behavior,                               BindingParameterCollection parameters)    {        ChannelFactory<IAudit> fact = new ChannelFactory<IAudit>("ClientAudit");        IAudit prt = fact.CreateChannel();        ClientAuditMessageInspector insp = new ClientAuditMessageInspector(prt);        behavior.MessageInspectors.Add(insp);    } }


The following shows the Audit attribute being applied to the IChat interface:

[System.ServiceModel.ServiceContractAttribute()] [ClientAuditBehavior.Audit] public interface IChat


Implementing behaviors as attributes is important for scenarios in which behaviors will always be required. It excludes the possibility of changes being made in the configuration that could lead to undesirable and/or unexpected results.

Configuring Behaviors in Code

You can configure behaviors in code by adding them to the appropriate Behaviors collection. The following example shows how to add a Service Behavior:

ServiceHost sh = new ServiceHost(typeof(Chat),baseServiceAddress);   sh.Description.Behaviors.Add(new ServiceAuditBehavior.AuditAttribute()); sh.Open();


Configuring Behaviors in the Configuration File

To enable your own behaviors within the configuration file, you will need to add your behavior to the behaviorElementExtensions section. The following is an example of how this could be done with the behaviors created earlier in the chapter:

<system.serviceModel>    <extensions>        <behaviorElementExtensions>            <add name="clientAuditBehavior" type="ClientAuditBehavior, Audit,                Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />        </behaviorElementExtensions>    </extensions> </system.serviceModel>


After the element and its configuration type are defined, the extension can be used, as is illustrated here:

<behaviors>     <behavior configurationName="ClientBehavior">         <clientAuditBehavior />     </behavior> </behaviors>


Creating a Behavior Extension

Placing behaviors in the configuration file provides more flexibility. This allows for adaptability and changes in deployment.

The following creates a behavior extension called ServiceAuditBehavior and adds the capability to configure it using a property named Enabled:

using System; using System.Collections.Generic; using System.Text; using System.ServiceModel.Configuration; namespace ServiceAuditBehavior {     class ServiceAuditBehaviorExtension: BehaviorExtensionSection     {         public override string ConfiguredSectionName         {             get { return "ServiceAuditBehavior"; }         }         [ConfigurationProperty("Enabled")]         public string Enabled         {             get { return (string)base["Enabled"]; }             set { base["Enabled"] = value; }         }         public override object CreateBehavior()         {             ServiceAuditBehavior behavior = new ServiceAuditBehavior();             behavior.Enabled = Enabled;             return behavior;         }     } }





Presenting Microsoft Communication Foundation. Hands-on.
Microsoft Windows Communication Foundation: Hands-on
ISBN: 0672328771
EAN: 2147483647
Year: 2006
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net