Reloading configuration without restarting application using ConfigurationManager.RefreshSection Reloading configuration without restarting application using ConfigurationManager.RefreshSection asp.net asp.net

Reloading configuration without restarting application using ConfigurationManager.RefreshSection


Make sure you are passing the correct case sensitive value to RefreshSection, i.e.

ConfigurationManager.RefreshSection("appSettings");


This seems to be a flaw (maybe a bug) when using an external config file for your appSettings. I've tried it using the configSource attribute and RefreshSection simply never works, I'm assuming this is the same when using the file attribute. If you move your appSettings back inside your web.config RefreshSection will work perfectly but otherwise I'm afraid you're doomed.


For some reason ConfigurationManager.RefreshSection("appSettings") wasn't working for me. Reloading the Web.Config into a Configuration object seems to work correctly. The following code assumes the Web.Config file is one directory below the executing (bin) folder.

ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();Uri uriAssemblyFolder = new Uri(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase));string appPath = uriAssemblyFolder.LocalPath;configMap.ExeConfigFilename = appPath + @"\..\" + "Web.config";Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None); 

And is used like:

string webConfigVariable = config.AppSettings.Settings["webConfigVariable"].Value;