Custom WCF Behaviors Through App.Config

You can create extension elements in your WCF configuration files in order to support introducing custom behaviors (such as IEndpointBehavior-implementations) into the processing pipeline in a declarative manner. However, the documentation online at this moment isn't too clear on the issue (well, it's outdated, so of course it's not clear!).

Doing it fortunately is not hard; here's how to:

1. Create your custom behavior configuration element by creating a class that derives from the System.ServiceModel.Configuration.BehaviorExtensionElement class.

2. Add any properties you need to the class to represent attributes in the configuration element, and mark them with the [ConfigurationElement] attribute. For example:

/// <summary>

/// Notice to add to the service

/// WSDL and XSD

/// </summary>

[ConfigurationProperty("notice", DefaultValue="", IsRequired=true)]

public string Notice

{

   get { return (string)base["notice"]; }

   set { base["notice"] = value; }

}

3. Override the BehaviorType property and return the type of the behavior your configuration element will introduce into the pipeline:

/// <summary>
///
Return the type of the behavior we configure
///
</summary>
public
override Type BehaviorType
{
  
get { return typeof(WsdlNoticeBehavior); }
}

4. Override the CreateBehavior() method; this is where you create and return an instance of your custom behavior based on the properties you hold from the configuration element:

/// <summary>
/// Create an instance of the behavior
/// we represent
/// </summary>
/// <returns>The WsdlNoticeBehavior instance</returns>
protected override object CreateBehavior()
{
   return new WsdlNoticeBehavior(Notice);
}

5. You'll likely want to override the Properties property and the CopyFrom() method as well:

private ConfigurationPropertyCollection _properties; 
/// <summary>
/// Return a collection of all our properties
/// </summary>
protected override ConfigurationPropertyCollection Properties
{
  
get
  
{
     
if ( _properties == null )
     
{
        
_properties =
new ConfigurationPropertyCollection();
        
_properties.Add(
new ConfigurationProperty(
           
"notice",
typeof(string), "",
           
ConfigurationPropertyOptions.IsRequired
           
));
     
}
     
return _properties;
  
}
}
/// <summary>
///
Copy the information of another element into
///
ourselves
///
</summary>
///
<param name="from">The element from which to copy</param>
public
override void CopyFrom(ServiceModelExtensionElement from)
{
  
base.CopyFrom(from);
  
WsdlNoticeElement element = (WsdlNoticeElement)from;
  
Notice = element.Notice;
}

After this, you're ready to introduce your behavior into the configuration file. First, you need to register your custom behavior configuration element by adding it in the <system.servicemodel>/<extensions>/<behaviorExtensions> element:

<extensions>
  
<behaviorExtensions>
     
<add name="wsdlNotice"
          
type
="Winterdom.ServiceModel.Extensions.Configuration.WsdlNoticeElement,
Winterdom.ServiceModel.Extensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"

          
/>
  
</behaviorExtensions>
</
extensions>

Now you can use your custom element in the correct behaviors collection:

<behaviors>
  
<endpointBehaviors>
   
 
<behavior name="EndpointBehaviors">
        
<wsdlNotice notice="This is a notice"/>
     
</behavior>
  
</endpointBehaviors>
</
behaviors>

That's it!

3 Comments

  1. Ross Holder

    I’ve been playing around with this stuff at work a fair bit lately, and am intrigued. However, for what I’ve in mind to do with behaviors – I’ll need to have the ability to register a single stackElement with multiple <behavior …/> tags. Is this possible? If not what other approaches can one undertake to achieve the desired effect?
    (BTW – have tried creating additional stackElement instances of the same type and inserting them wtihin multiple endpoint behaviors and was told by SvcConfig that this isn’t allowed.)
    Gratefully,
    RH

  2. Pingback: WCF Security: WCF Performance & ProtectionLevel – Part 2 « Adil Akhter's Weblog

  3. Nadav Yeheskel

    If you are having trouble with your service and you are getting the following error:
    “An error occurred creating the configuration section handler for system.serviceModel/behaviors: ‘XXXXXXXX’ Extension element cannot be added to this element. Verify that the extension is registered in the extension collection at system.serviceModel/extensions/behaviorExtensions.”

    Look at this link:
    http://connect.microsoft.com/wcf/feedback/details/216431/wcf-fails-to-find-custom-behaviorextensionelement-if-type-attribute-doesnt-match-exactly

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>