« MVP 2006 | Main | Inline XSD in WSDL with WCF »
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!
WCF, Windows Communication Foundation, WCF Extensibility
Tomas Restrepo is a software developer located in Colombia, South America. His interests include .NET, Connected Systems, PowerShell and lately dynamic programming languages. More...
email: tomas@winterdom.com msn: tomasr@passport.com
Copyright © 2002-2008, Tomas Restrepo.
Powered by: newtelligence dasBlog 2.2.8279.16125