Updating Configuration Settings at Run Time

The default configuration system included in the Enterprise Library Core is, with one exception, read-only. When using application configuration files (App.config and Web.config), updates to these files do not affect existing instances of objects. However, new instances created after changes are made to the configuration file will reflect these changes. In Windows Forms applications, you can restart the application to cause it to read the all of the new configuration information. Web Forms (ASP.NET) applications will detect and reload the configuration information, but the standard behavior of ASP.NET causes the application to restart when you edit the configuration file, which causes all state to be lost for the application.

The one exception to this is the Logging Application Block, which is able to detect configuration changes and reload the configuration without restarting the application. This works for Web Forms and Windows Forms applications, though it will still automatically trigger the application to restart for Web Forms applications. This means that you cannot rely on maintenance of in-process session state in ASP.NET applications when you change the configuration file.

Detecting Configuration Changes

You can detect changes to the configuration by registering for the SourceChanged event that is defined in the IConfigurationSource interface and implemented by all Enterprise Library configuration sources. You can register and unregister for this event using the standard event handling approach, as shown in the following example that detects changes to configuration stored in a custom file named MyConfig.config.

private void Form1_Load(Object sender, EventArgs e)
{
  IConfigurationSource source = new FileConfigurationSource("MyConfig.config");
  source.SourceChanged += MyConfigChangedEventHandler;
}
 
private void MyConfigChangedEventHandler(Object sender, ConfigurationChangedEventArgs e)
{
  MessageBox.Show(String.Format("Detected change in section: {0}", e.SectionName));
}
'Usage
Private Sub Form1_Load(sender As Object sender, e As EventArgs)
  Dim source As IConfigurationSource = New FileConfigurationSource("MyConfig.config")
  AddHandler source.SourceChanged, AddressOf MyConfigChangedEventHandler
End Sub
 
Private Sub MyConfigChangedEventHandler(sender As Object, e As ConfigurationChangedEventArgs)
  MessageBox.Show(String.Format("Detected change in section: {0}", e.SectionName))
End Sub

The IConfigurationSource interface also defines two methods, AddSectionChangeHandler and RemoveSectionChangeHandler, which you can use to register and unregister for this event. All configuration sources implement these methods, which take as parameters the name of the section to monitor and a reference to the handler to execute when the section changes.

Note

If you register a handler to detect changes to a section, it is important to remove it before the configuration source goes out of scope. If not, it will not be garbage-collected because there is still a strong reference to the event handler.