we’re configuring parts of an application at runtime:
ConfigurationManager.AppSettings["someKey"] = "someValue";
This code is called on init of an http module, so it’s very early and it works fine – almost.
The reason is for an framework, where we (guys providing the framework) want to avoid that the users of the framework (some other dev’s in our company)
- forget adding this setting to the web.config or
- add wrong values in their specific web.configs
Now, under some circumstances (that is, i.e. high memory pressure) the ConfigurationManager obviously drops the NameValueCollection for the appSettings and reloads the values from the file, so that the key/value we added by code is lost.
This happens sometimes after the web application runs fine for several hours. It is very rare and happens only when the web application seems to be under heavy load.
Now the question is:
Is there a way to
- prevent the Configuration manager dropping and reloading the appSettings (this would include calls to
RefreshSection) or - detect when the configuration manager drops its appSettings section, so that we can react and add the key again or
- is there any other means to put a value into appSettings by code and persisting it until the application restarts the next time, without actually changing the web.config file?
Update: Determined the reason why the values are dropped (memory pressure)
The solution that Kevin P. Rice offered in his comment is great for this problem.
It’s answered here: Is there a way to override ConfigurationManager.AppSettings?