Separating out WCF Configuration Into Multiple Files with configSource

WCF configuration files can sometimes be very, very long. And there’s a good reason for that too: WCF makes heavy use of configuration so that you can do just about anything you can do with code in a config file. I recently decided to estimate how much of the entire framework’s configuration belongs to WCF by looking at what percentage of the framework’s schema is under system.serviceModel, system.runtime.serialization, and system.serviceModel.activation. It turns out those section groups alone account for more than 70% of the entire framework’s configuration schema.

 

So even more so than for other section groups, it’s useful to be able to break up a long WCF configuration file into smaller, more manageable files. You can do this using the “configSource” attribute. It allows you to reference another file that contains the configuration section you declared.

 

So instead of having the following in your App.config:

 

<configuration>

  <system.serviceModel>

    <services>

      <service name="Service.EchoService" behaviorConfiguration="EchoServiceBehavior">

        <endpoint address ="" binding="basicHttpBinding" contract="Service.IEcho"/>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="EchoServiceBehavior">

          <serviceMetadata httpGetEnabled="True" />

          <serviceDebug includeExceptionDetailInFaults="True" />

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

</configuration>

 

You could have the following in your App.config:

 

<configuration>

  <system.serviceModel>

    <services>

      <service name="Service.EchoService" behaviorConfiguration="EchoServiceBehavior">

        <endpoint address ="" binding="basicHttpBinding" contract="Service.IEcho"/>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

      </service>

    </services>

    <behaviors configSource="behaviors.config" />

  </system.serviceModel>

</configuration>

 

And this in behaviors.config:

 

<behaviors>

  <serviceBehaviors>

    <behavior name="EchoServiceBehavior">

      <serviceMetadata httpGetEnabled="True" />

      <serviceDebug includeExceptionDetailInFaults="True" />

    </behavior>

  </serviceBehaviors>

</behaviors>

 

If you wanted you could separate out your entire system.serviceModel section group into smaller files:

 

<configuration>

  <system.serviceModel>

    <services configSource="services.config"/>

    <bindings configSource="bindings.config"/>

    <behaviors configSource="behaviors.config"/>

    <extensions configSource="extensions.config"/>

  </system.serviceModel>

</configuration>

 

Note, however, that you can only use this for sections. This technique can’t be used for the system.serviceModel section group or for any config elements. Also note that the referenced files should contain only one section, and that you won’t need a <configuration> or <system.serviceModel> tag in them, just the section being referenced.