Reading of WCF configuration

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

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.

<MySection>
 <
dynamicEndpoints>
   <
dynamicEndpoint name="MyTestEndpoint1"
                    behaviorConfiguration="SomeBehavior"/>
 </
dynamicEndpoints>
</
MySection >

. . .

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

. . .


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

ServiceModelSectionGroup serviceModelGroup =
          cfg.GetSectionGroup("system.serviceModel"
               
as ServiceModelSectionGroup;

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

            foreach (EndpointBehaviorElement behavior in bhvs)
            {
                if (behavior.Name == SomeBehavior")
                {
                   m_Dict.Add(endpoint.Name,
                   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:
developers.de is a .Net Community Blog powered by daenet GmbH.