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.

4
  • What's the full error? Is your connection.config copied to the same directory as web.config? Commented Nov 17, 2016 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. Commented Nov 17, 2016 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. Commented Nov 19, 2016 at 20:31
  • A possible solution is to use a post-build step to copy connection.config next to the web.config. Commented Nov 19, 2016 at 20:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.