1

I have a large ASP.NET MVC project with a data access layer that uses Entity Framework. As well, I have a Windows service project (in a different solution) that makes use of the same DAL project. I have my Web.config/App.config setup so that the connection strings are loaded from a separate connection.config file that is in a shared location. The connection configuration is linked (Add Existing Item > Add As Link) in both projects with Build Action = Content and the Copy To Output Directory = Copy if newer.

The Windows service project runs with no issues, I can see the service connecting to my database correctly. However the ASP.NET MVC project throws an excpetion saying Unable to open configSource file 'connection.config'. (C:\Path\To\My\web.config line 9).

I checked that the connection.config file is copied to output directory. I also tried making a copy of the connection.config file in the ASP.NET project (instead of linking it) and this runs just fine. Why does linking the file suddenly cause an exception?

My files below for reference

App.config (Windows service)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <connectionStrings configSource="connection.config" />
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  [...]
  <connectionStrings configSource="connection.config" />
  [...]
</configuration>

connection.config

<?xml version="1.0"?>
<connectionStrings>
  <clear />
    <add name="MyConnection" connectionString="Data Source=./localhost;Initial Catalog=MyBatabase;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>

Edit

I came across this answer that works, however I'm concerned how this would affect the application in release mode or when it's deployed. As well I still don't understand why having the file linked has a different behavior than when it's copied in.

Community
  • 1
  • 1
amura.cxg
  • 2,348
  • 3
  • 21
  • 44
  • What's the full error? Is your _connection.config_ copied to the same directory as _web.config_? – Jasen Nov 17 '16 at 21:34
  • That is the full error text of the inner exception, the wrapping exception message simply says `The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception`. The `web.config` isn't copied to the output at all. – amura.cxg Nov 17 '16 at 21:48
  • _web.config_ doesn't get copied to the bin folder; it is published to the application web root. When you Copy to Output Directory, the file is copied to the bin folder. Your web app is trying to locate the _connections.config_ in the same folder as your _web.config_ but it can't find it. For the Windows service, it locates the config file in bin which is why it works there. – Jasen Nov 19 '16 at 20:31
  • A possible solution is to use a post-build step to copy _connection.config_ next to the _web.config_. – Jasen Nov 19 '16 at 20:35

0 Answers0