views:

70

answers:

3

I need to reload the configuration file after modifying it. How this can be done using appdomains? A code sample would be useful. Thanks.

+3  A: 

ConfigurationManager.RefreshSection might work for you.

Fredrik Mörk
A: 

Yes, it's possible... depending on HOW you access your configuration file.

If you rely on the default behavior, then the answer is NO.

However, if you access the configuration through a static property of method common to your project, then it's possible to reload it.

I don't have the code snippet with me now, but I did something similar even using a FileSystemWatcher to detect changes in the config file.

There's one caveat, it works only with properties that you access directly through your code, automatic configuration will not be reloaded when you do such thing.

Paulo Santos
A: 

I have actually found the solution to the given problem. Below are fiew lines of code of how this can be done:

            AppDomainSetup domaininfo = new AppDomainSetup();

            domaininfo.ApplicationBase = System.Environment.CurrentDirectory;
            domaininfo.ConfigurationFile = "Target_Config.exe.config";

            Evidence adevidence = AppDomain.CurrentDomain.Evidence;

            AppDomain dom = AppDomain.CreateDomain("test", adevidence, domaininfo);

            var someType =(SomeType)dom.CreateInstanceAndUnwrap("Target_Assembly", 
                "Target_Assembly.SomeType");

The key point here is the AppDomainSetup class, which allows to set the configurationfile property on the assembly to be created. Now, we can monitor the configuration file "Target_Config.exe.config" for changes. When it is changed, the above created appdomain should be unloaded and then recreated.

Markus
This is probably a solution to reusing a configuration file across several applications without copying it for each application.
Markus