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:

///

/// Notice to add to the service

/// WSDL and XSD

///

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

/// 

/// Return the type of the behavior we configure
///

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:

/// 

/// Create an instance of the behavior
/// we represent
///

/// The WsdlNoticeBehavior instance
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; 
/// 

/// Return a collection of all our properties
///

protected override ConfigurationPropertyCollection Properties
{
  
get
  
{
     
if ( _properties == null )
     
{
        
_properties =
new ConfigurationPropertyCollection();
        
_properties.Add(
new ConfigurationProperty(
           
"notice",
typeof(string), "",
           
ConfigurationPropertyOptions.IsRequired
           
));
     
}
     
return _properties;
  
}
}
/// 

/// Copy the information of another element into
///
ourselves
///

///
The element from which to copy
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 // 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!


Tomas Restrepo

Software developer located in Colombia.