Reading of WCF configuration

Damir Dobric Posts

Next talks:



Follow me on Twitter: #ddobric




Sometimes it is required to read the WCF specific information contained in the application's configuration file.
Usually, there is no need to do that by utilizing of .NET configuration system. However, imagine scenario which should
Allow you to implement some very special kind of the endpoint. Some people could find this idea very theoretically, by believe
me it is more practical than it looks.

For example, image you need the endpoint on the client which does not have an endpoint address. Unfortunately, the WCF endpoint element requires
to specify the address, because its attribute IsRequired is set on true. The bad thing is that there is not possibility to extend the endpoint element like for example
behavior extension. In such cases you will define you custom section which will read WCF standard settings.
Next example is a custom configuration section which references the standard WCF behavior.

dynamicEndpoint name="MyTestEndpoint1"
MySection >

. . .

       <behavior name="TestServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />    
      <behavior name="SomeBahavior">
myBehavior . . .>

. . .

Following code snippet shows how to read elements and properties of the given configuration.

ServiceModelSectionGroup serviceModelGroup =
as ServiceModelSectionGroup;

(serviceModelGroup != null)
    if(cfg.GetSectionGroup("system.serviceModel").Sections["behaviors"] !=   
        Properties["endpointBehaviors"] != null)
            EndpointBehaviorElementCollection bhvs =
            ElementInformation.Properties["endpointBehaviors"].Value as

            foreach (EndpointBehaviorElement behavior in bhvs)
                if (behavior.Name == SomeBehavior")
                   behavior.ElementInformation.Properties["myBehavior "].Value
                   as MyBehaviorExtension);

One full .NET 2.0 configuration example example can be downloaded here. Another intersting example about development
of custo sections and elements can be found here. Hope this help you to more understand the powerfull,but not very documented configuration system.

Posted Feb 07 2007, 11:32 PM by Damir Dobric
Filed under: is a .Net Community Blog powered by daenet GmbH.