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.