Dynamically Loading Config Files

posted on 09/08/09 at 12:52:01 am by Joel Ross

In my last post, I talked about dynamically loading and unloading DLLs. It works well, but I quickly ran into an issue: configuration data. The main application, which is what houses the DLL that's loaded dynamically, has a lot of configuration data, and I need that to do the processing I need to do.

Since one of the main requirements for the secondary application was that it had to have near 100% uptime, I couldn't include all of the main app's configuration in the secondary app's config - because it might get updated with new builds, which would require the secondary app to restart. So, I started looking at how you can load a configuration file dynamically. As usual, it's pretty straightforward:

   1: var map = new ExeConfigurationFileMap {ExeConfigFilename = @"C:\Bins\app.config"};
   2: ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

Now, the DLL can load its own configuration information on the fly. For my application, I added a method to my BaseType abstract class:

   1: public abstract void InitializeConfig(string configPath);

By implementing this method, I can now initialize the config from the secondary application after it loads the DLL, and all the secondary app has to know about is the location of the config file the main application uses.

Categories: Development, C#