wcfconfigurationwcf-configurationservicebehavior

Adding attribute to BehaviorExtensionElement


I'm adding a custom behaviorExtensionElement for WCF and want to add an attribute that can be read when the configured element is being read, e.g.

<system.serviceModel>
    <extensions>
      <behaviorExtensions>
        <add name="myExtension"
             type="Bar.FooBarElement, Bar"/>
      </behaviorExtensions>
    </extensions>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <myExtension myAttribute="Foo" />

However, I get an error "Unrecognized attribute 'myAttribute'. Note that attribute names are case-sensitive."

How can I avoid this? How do I read the myAttribute value in code?


Solution

  • Turns out it's pretty easy, since BehaviorExtensionElement subclasses ConfigurationElement, the usual configuration rules apply.

    [ConfigurationProperty("myAttribute")]
    public string MyAttribute
    {
      get { return (string)this["myAttribute"]; }
      set { this["myAttribute"] = value; }
    }