How to read app.config from another assembly?

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.


.NET will only load at most one App.config file for an executing assembly. If your satellite assemblies have App.config files, they will not be parsed by the executing assembly.

In order to get the settings from the satellite assembly's App.config you must move (copy) those settings into your executing assembly's App.config.


I solved it like this. Assuming your assembly with the connection string in it's .config file is a reference in your project and so exists along side the executing assembly.

String AssemblyName = "MyAssemblyWithConfig.dll";
String appConfigPath = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\" + AssemblyName + ".config";
System.AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", appConfigPath);
string connectionString = ConfigurationManager.ConnectionStrings["TestEntities"].ConnectionString;

Tags:

C#

App Config