1

Consider the following:

  • A windows service with a config file and the setting Engine.Url
  • The service loads an assembly into its own AppDomain
  • Code in the the assembly needs the setting Engine.Url

    string s = ConfigurationManager.AppSettings["Engine.Url"]

does not work, s will be null.

Then I tried

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string engineUrl = config.AppSettings["Engine.Url"];

this doesnt compile with the error: 'System.Configuration.ConfigurationElement.this[System.Configuration.ConfigurationProperty]' is inaccessible due to its protection level

Is there any way to get to the standard config file from within an AppDomain?

edit:

This doesn't work either, engineUrl will be null:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationElement engineUrl = config.AppSettings.Settings["Engine.Url"];
Travis
  • 10,444
  • 2
  • 28
  • 48
Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98
  • Have a look at the answer to [this question](http://stackoverflow.com/questions/6150644/change-default-app-config-at-runtime). It is a bit more work, but it will allow you to load the service's config file at runtime so that the assembly can access its configuration. – Sameer Singh Jan 22 '14 at 21:12

1 Answers1

1

Simple answer: Yes, but it's wonky.

You have a couple options. You can set, I believe, the ConfigurationFile property of an AppDomainSetup object used when creating your AppDomain.

You can call AppDomain.SetData("APP_CONFIG_FILE", path_to_file) (see the related MSDN page)

Travis
  • 10,444
  • 2
  • 28
  • 48
  • 1
    this doesn't work everywhere...there's tons of places that cache the loaded the configuration...including ConfigurationManager internal. See ResetConfigurationMechanism (which still doesn't work globally) – Jeff Jan 22 '14 at 22:16
  • Yeah, exactly why I said it's wonky. I guess I could have expanded upon that a bit. – Travis Jan 22 '14 at 23:39