Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between using AppDomain and ConfigurationManager when getting the path to a configuration file?

When getting the file path of the current configuration file, what is the difference in using the AppDomain namespace and the ConfigurationManager namespace? And when would you use one over the other?

For example, both return the same path:

string f1 = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

string f2 = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None ).FilePath;
like image 775
Metro Smurf Avatar asked Aug 02 '11 22:08

Metro Smurf


1 Answers

It comes down to when and why you want a configuration file. The gist of it is this:

  • The ConfigurationManager will pull the configuration file for the application itself (optionally restricted by the current user).
  • The AppDomain will hand back whatever configuration file it was loaded with (which, in some cases, may be the same file as the application).

As a rough example, let's take a hypothetical application that can use plugins that it adds/removes on the fly. You don't want to have to keep those plugin assemblies in memory for the life of the application, that would defeat the purpose, so you create a separate AppDomain within your application. It will handle the loading and communication between the app and the plugin assemblies, do whatever you need to do with them and the app can unload the assemblies by dropping the AppDomain whenever it needs to.

The plugin AppDomain has quite a few settings that you'd rather keep separate from the client configuration file, so when you create the AppDomain you specify the separate file location. Within that AppDomain, the configuration file is that file.

The client configuration, though, may depend on who's using it (and they may have the ability to change it and customize their settings). You'd rather use an application-wide configuration that's segregated by a given user instead and not even give them the option to mess with the plugin-specific settings (or other user's settings). The ConfigurationManager could theoretically pull from any number of files.

That's a very general idea that handwaves away all of the implementation, but hopefully that starts to illustrates how the two might differ.

Here are the MSDN pages for both the AppDomainSetup and OpenExeConfiguration() as well, which may be useful and has additional links to configuration-related resources.

like image 152
Chris Hannon Avatar answered Sep 20 '22 00:09

Chris Hannon