C# – Plugin to use its own app.config

app-configcpluginssettings

I finally managed to build a working solution of a plugin architecture with help of some guys over here, but now a new problem arises.

My hosting application uses it's app.config file for some defaults for the executing assembly (which is a Windows Service).

Each plugin should be able to load it's own settings from a separate plugin settings file because the host shouldn't be made aware of the plugin settings. In the plugin project I added an app.config file as well (with some settings and a connection string) so that I can instantiate the Properties.Settings class and use it's properties in the plugin code.

The problem is when I change the settings in the app.config of the plugin (which is build as plugin.dll.config) I can't see those changes in the plugin itself, which still uses the design time settings.

Is there a way to load the app.config settings in each plugin so the generated Properties.Settings class will work? If not is there another way to load a app.config based settings file into the plugin? I'm planning on adding a LoadConfiguration method in the IPlugin interface so each plugin will load it's own settings.

Best Answer

You're working against the architecture of app.config. You get one app.config file per executable (EXE, not DLL). The executable launches, creates its AppDomain, and then loads MyApp.exe.config. You can add app.config objects all you want in Visual Studio, but they are ignored for DLLs. I think what you want to do is manually copy the XML from the dll.config and paste it into the application level app.config. (I'm sure there's a way to automate this using TeamBuild or some such.) The overridden values will then be available to your Properties.Settings class.