0

I've two separate App.Config files that work perfectly.

The configuration file containing the database connection:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
  <connectionStrings><add name="EmsDatabaseEntities" connectionString="metadata=res://*/Data.EmsEntityModel.csdl|res://*/Data.EmsEntityModel.ssdl|res://*/Data.EmsEntityModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\SQL2008EXPRESS;initial catalog=EmsDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /><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=&quot;Data Source=|DataDirectory|\Data\LocalEmsDatabase.sdf&quot;" providerName="System.Data.EntityClient" /></connectionStrings>    
</configuration>

The configuration file containing the WCF settings:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CheckResultServiceBehavior">
          <serviceMetadata httpGetEnabled="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="Ems.CheckResultService" behaviorConfiguration="CheckResultServiceBehavior">
        <endpoint
           address=""
           binding="netTcpBinding"
           bindingConfiguration="tcpConfig"
           contract="Maha.AIP.Ems.WcfContracts.ICheckResultService_1_0"/>
        <endpoint
          address="mex"
          binding="mexTcpBinding"
          bindingConfiguration=""
          contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8080/CheckResultService_1_0/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <bindings>
      <netTcpBinding>
        <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">
          <security mode="None"/>
        </binding>
      </netTcpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

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.

The whole file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
  <connectionStrings><add name="EmsDatabaseEntities" connectionString="metadata=res://*/Data.EmsEntityModel.csdl|res://*/Data.EmsEntityModel.ssdl|res://*/Data.EmsEntityModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\SQL2008EXPRESS;initial catalog=EmsDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /><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=&quot;Data Source=|DataDirectory|\Data\LocalEmsDatabase.sdf&quot;" providerName="System.Data.EntityClient" /></connectionStrings>

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CheckResultServiceBehavior">
          <serviceMetadata httpGetEnabled="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="Ems.CheckResultService" behaviorConfiguration="CheckResultServiceBehavior">
        <endpoint
           address=""
           binding="netTcpBinding"
           bindingConfiguration="tcpConfig"
           contract="Maha.AIP.Ems.WcfContracts.ICheckResultService_1_0"/>
        <endpoint
          address="mex"
          binding="mexTcpBinding"
          bindingConfiguration=""
          contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8080/CheckResultService_1_0/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <bindings>
      <netTcpBinding>
        <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">
          <security mode="None"/>
        </binding>
      </netTcpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

What do I have to do to put both configurations into one file and make it work?

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.

Loading WCF config (for server + client) from custom source (not standard XML) http://blogs.msdn.com/b/dotnetinterop/archive/2008/09/22/custom-service-config-file-for-a-wcf-service-hosted-in-iis.aspx http://msdn.microsoft.com/en-us/library/aa395224(v=vs.100).aspx

I hope there is a simple way to put both configurations into only one App.config file.

Community
  • 1
  • 1
Sebastian Schumann
  • 3,204
  • 19
  • 37
  • while combining both configuration files, how are you using/accessing the connection string to do database connection ? – Milan Raval Jan 13 '14 at 08:56
  • if you access it like below it shouldn't be any problem - ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString – Milan Raval Jan 13 '14 at 08:59
  • Thx for the hint. It solves the problem. We forwarded the name of the connectionstring to the ObjectContext of System.Data. We could forward the whole string that works. – Sebastian Schumann Jan 13 '14 at 11:20
  • plz mark it as answer - if it has solved your problem – Milan Raval Jan 15 '14 at 06:55

1 Answers1

1

if you access it like below it shouldn't be any problem - ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString

Milan Raval
  • 1,880
  • 1
  • 16
  • 33