title: App.Config ConnectionString for DataBase and WCF in same file
body: <p>I've two separate App.Config files that work perfectly.</p>
  
  <p>The configuration file containing the database connection:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;startup useLegacyV2RuntimeActivationPolicy="true"&gt;
      &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /&gt;
    &lt;/startup&gt;
    &lt;connectionStrings&gt;&lt;add name="EmsDatabaseEntities" connectionString="metadata=res://*/Data.EmsEntityModel.csdl|res://*/Data.EmsEntityModel.ssdl|res://*/Data.EmsEntityModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=localhost\SQL2008EXPRESS;initial catalog=EmsDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&amp;quot;" providerName="System.Data.EntityClient" /&gt;&lt;add name="LocalEmsDatabaseEntities" connectionString="metadata=res://*/Data.LocalEmsEnityModel.csdl|res://*/Data.LocalEmsEnityModel.ssdl|res://*/Data.LocalEmsEnityModel.msl;provider=System.Data.SqlServerCe.3.5;provider connection string=&amp;quot;Data Source=|DataDirectory|\Data\LocalEmsDatabase.sdf&amp;quot;" providerName="System.Data.EntityClient" /&gt;&lt;/connectionStrings&gt;    
  &lt;/configuration&gt;
  </code></pre>
  
  <p>The configuration file containing the WCF settings:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;system.serviceModel&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="CheckResultServiceBehavior"&gt;
            &lt;serviceMetadata httpGetEnabled="false"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;services&gt;
        &lt;service name="Ems.CheckResultService" behaviorConfiguration="CheckResultServiceBehavior"&gt;
          &lt;endpoint
             address=""
             binding="netTcpBinding"
             bindingConfiguration="tcpConfig"
             contract="Maha.AIP.Ems.WcfContracts.ICheckResultService_1_0"/&gt;
          &lt;endpoint
            address="mex"
            binding="mexTcpBinding"
            bindingConfiguration=""
            contract="IMetadataExchange"/&gt;
          &lt;host&gt;
            &lt;baseAddresses&gt;
              &lt;add baseAddress="net.tcp://localhost:8080/CheckResultService_1_0/"/&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;bindings&gt;
        &lt;netTcpBinding&gt;
          &lt;binding
            name="tcpConfig"
            openTimeout="00:01:00"
            closeTimeout="00:01:00"
            sendTimeout="01:00:00"
            receiveTimeout="01:00:00"
            maxBufferSize="2147483647"
            maxReceivedMessageSize="2147483647"
            maxBufferPoolSize="2147483647"&gt;
            &lt;security mode="None"/&gt;
          &lt;/binding&gt;
        &lt;/netTcpBinding&gt;
      &lt;/bindings&gt;
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>But the configuration doesn’t work if I copy the content of both files together into one file. It depends on which configuration is on top of the file which part of the configuration could be read. If the WCF configuration is on top it will work but I couldn’t connect to the database. If the database connection is on top it will work but I couldn’t open the service host.</p>
  
  <p>The whole file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;startup useLegacyV2RuntimeActivationPolicy="true"&gt;
      &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /&gt;
    &lt;/startup&gt;
    &lt;connectionStrings&gt;&lt;add name="EmsDatabaseEntities" connectionString="metadata=res://*/Data.EmsEntityModel.csdl|res://*/Data.EmsEntityModel.ssdl|res://*/Data.EmsEntityModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=localhost\SQL2008EXPRESS;initial catalog=EmsDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&amp;quot;" providerName="System.Data.EntityClient" /&gt;&lt;add name="LocalEmsDatabaseEntities" connectionString="metadata=res://*/Data.LocalEmsEnityModel.csdl|res://*/Data.LocalEmsEnityModel.ssdl|res://*/Data.LocalEmsEnityModel.msl;provider=System.Data.SqlServerCe.3.5;provider connection string=&amp;quot;Data Source=|DataDirectory|\Data\LocalEmsDatabase.sdf&amp;quot;" providerName="System.Data.EntityClient" /&gt;&lt;/connectionStrings&gt;
  
    &lt;system.serviceModel&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="CheckResultServiceBehavior"&gt;
            &lt;serviceMetadata httpGetEnabled="false"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;services&gt;
        &lt;service name="Ems.CheckResultService" behaviorConfiguration="CheckResultServiceBehavior"&gt;
          &lt;endpoint
             address=""
             binding="netTcpBinding"
             bindingConfiguration="tcpConfig"
             contract="Maha.AIP.Ems.WcfContracts.ICheckResultService_1_0"/&gt;
          &lt;endpoint
            address="mex"
            binding="mexTcpBinding"
            bindingConfiguration=""
            contract="IMetadataExchange"/&gt;
          &lt;host&gt;
            &lt;baseAddresses&gt;
              &lt;add baseAddress="net.tcp://localhost:8080/CheckResultService_1_0/"/&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;bindings&gt;
        &lt;netTcpBinding&gt;
          &lt;binding
            name="tcpConfig"
            openTimeout="00:01:00"
            closeTimeout="00:01:00"
            sendTimeout="01:00:00"
            receiveTimeout="01:00:00"
            maxBufferSize="2147483647"
            maxReceivedMessageSize="2147483647"
            maxBufferPoolSize="2147483647"&gt;
            &lt;security mode="None"/&gt;
          &lt;/binding&gt;
        &lt;/netTcpBinding&gt;
      &lt;/bindings&gt;
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>What do I have to do to put both configurations into one file and make it work?</p>
  
  <p>I tried to find a simple way to use a custom WCF configuration file but I only found the following links that need a lot of code to be able to read the file. I couldn’t believe that this amount of code is needed for the simple task to read configuration another file containing the same content.</p>
  
  <p><a href="https://stackoverflow.com/questions/312604/loading-wcf-config-for-server-client-from-custom-source-not-standard-xml">Loading WCF config (for server + client) from custom source (not standard XML)</a>
  <a href="http://blogs.msdn.com/b/dotnetinterop/archive/2008/09/22/custom-service-config-file-for-a-wcf-service-hosted-in-iis.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/b/dotnetinterop/archive/2008/09/22/custom-service-config-file-for-a-wcf-service-hosted-in-iis.aspx</a>
  <a href="http://msdn.microsoft.com/en-us/library/aa395224(v=vs.100).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa395224(v=vs.100).aspx</a></p>
  
  <p>I hope there is a simple way to put both configurations into only one App.config file.</p>