Search code examples
c#app-config

How to read app.config from another assembly?


I have two projects:

  • Console project (Test.exe)
  • Class Library project (Test.Data.dll)

My Class Library project contains an app.config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="TestEntities" connectionString="metadata=res://*/DBNews.csdl|res://*/DBNews.ssdl|res://*/DBNews.msl;provider=System.Data.SqlClient;provider connection string=&quot;{0}&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

From the Console project I want to access the settings from the Class Library, so I've tried:

var config = ConfigurationManager.OpenExeConfiguration("Test.Data.dll");
config.ConnectionStrings.ConnectionStrings[0].Name; // LocalSqlServer
// seems to be the wrong assembly.

And:

var config = ConfigurationManager.OpenExeConfiguration("Test.Data.dll.config");
// invalid exePath

How can I access the DLL's app.config ?


Solution

  • The DLL doesn't have its own app.config at runtime. The app.config is only there for the Entity Framework designer.

    During execution, the DLL will try to read the values from the Application's app.config file. For Entity Framework connections, that means you have to copy the connection information into the Application's app.config.