I used satellite response, worked fine in one IIS test environment, but not in another. It turns out that after you set the APP_CONFIG_FILE property, you may need to use reflection to touch the ConfigurationManager class to make changes. I used this function after setting the APP_CONFIG_FILE property:
private static void ResetConfiguration()
{
typeof(ConfigurationManager)
.GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, 0);
typeof(ConfigurationManager)
.GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, null);
typeof(ConfigurationManager)
.Assembly.GetTypes()
.Where(x => x.FullName == "System.Configuration.ClientConfigPaths")
.First()
.GetField("s_current", BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, null);
}
Other than that, it's probably worth saving the property first and then restoring it when you are done:
string oldConfigName = AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString();
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", oldConfigName);
ResetConfiguration();
source
share