Updating Configuration Settings at Run-time

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

The configuration system included in the Enterprise Library Core is read-only. When using application configuration files (App.config and Web.config), updates to these files are not detected at run time. Windows Forms applications must be restarted to read the configuration information. Web Forms (ASP.NET) applications will detect and reload the information, but this forces the application to restart and 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.

Saving Configuration to a Detached File

If you require updateable configuration settings for a Web Forms application or for other application blocks, the best option is to use an external file for the configuration. The following code shows how to save configuration so the section is saved to a detached file; you will need to make sure the configSource value is set before you add the section to the config.

class Program
{
  static void Main(string[] args)
  {
    ExeConfigurationFileMap map = new ExeConfigurationFileMap();
    map.ExeConfigFilename = "test.exe.config";
    Configuration config 
      = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
    MySection section = new MySection();
    section.Prop1 = "test";
    section.SectionInformation.ConfigSource = " Example.config";
    config.Sections.Add("mysection", section);
    config.Save();
  }

  public class MySection : ConfigurationSection
  {
    private const string Prop1PropertyName = "prop1";

    [ConfigurationProperty(Prop1PropertyName)]
    public string Prop1
    {
      get { return (string)this[Prop1PropertyName]; }
      set { this[Prop1PropertyName] = value; }
    }
  }
}
'Usage
Class Program

  Static Sub Main()
    Dim map As New ExeConfigurationFileMap()
    map.ExeConfigFilename = "test.exe.config"
    Dim config As Configuration _
      = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None)
    Dim section As New MySection()
    section.Prop1 = "test"
    section.SectionInformation.ConfigSource = "Example.config"
    config.Sections.Add("mysection", section)
    config.Save()
  End Sub

  Public Class MySection
    Inherits ConfigurationSection
    Private Const Prop1PropertyName As String = "prop1"

    <ConfigurationProperty(Prop1PropertyName)> _
    Public Property Prop1 As String
      Get
        Return CType(Me(Prop1PropertyName), String)
      End Get
      Set 
        Me(Prop1PropertyName) = value 
      End Set
    End Property
  End Class

End Class

Detecting Configuration Changes

If you use a custom file to store configuration information, you can detect changes to the file using a SectionChangeHandler. The following code shows how you can detect changes to Logging Application Block configuration in a custom file named Logging.config.

private void Form1_Load(Object sender, EventArgs e)
{
  IConfigurationSource source = new FileConfigurationSource("Logging.config");
  source.AddSectionChangeHandler("loggingConfiguration", ConfigChangedEvent);
}

private void ConfigChangedEvent(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("Logging.config")
  source.AddSectionChangeHandler("loggingConfiguration", ConfigChangedEvent)
End Sub

Private Sub ConfigChangedEvent(sender As Object, e As ConfigurationChangedEventArgs)
  MessageBox.Show(String.Format("Detected change in section: {0}", e.SectionName))
End Sub

It is important if you use the AddSectionChangeHandler method to add a handler to a section that you remove it using the RemoveSectionChangeHandler method 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.