Monitoring settings in a configsection of your app.config for changes

The usage:

public static void Main()
{
    using(var configSectionAdapter = new ConfigurationSectionAdapter<ACISSInstanceConfigSection>("MyConfigSectionName"))
    {
        configSectionAdapter.ConfigSectionChanged += () =>
        {
            Console.WriteLine("File has changed!  New setting is " + configSectionAdapter.ConfigSection.MyConfigSetting);
        };
        Console.WriteLine("The initial setting is " + configSectionAdapter.ConfigSection.MyConfigSetting);
        Console.ReadLine();
    }
}
 

The meat:

public class ConfigurationSectionAdapter<T> : IDisposable
    where T : ConfigurationSection
{
    private readonly string _configSectionName;
    private FileSystemWatcher _fileWatcher;

    public ConfigurationSectionAdapter(string configSectionName)
    {
        _configSectionName = configSectionName;
        StartFileWatcher();
    }

    private void StartFileWatcher()
    {
        var configurationFileDirectory = new FileInfo(Configuration.FilePath).Directory;
        _fileWatcher = new FileSystemWatcher(configurationFileDirectory.FullName);
        _fileWatcher.Changed += FileWatcherOnChanged; 
        _fileWatcher.EnableRaisingEvents = true;
    }

    private void FileWatcherOnChanged(object sender, FileSystemEventArgs args)
    {
        var changedFileIsConfigurationFile = string.Equals(args.FullPath, Configuration.FilePath, StringComparison.OrdinalIgnoreCase);
        if (!changedFileIsConfigurationFile)
            return;

        ClearCache();
        OnConfigSectionChanged();
    }

    private void ClearCache()
    {
        ConfigurationManager.RefreshSection(_configSectionName);
    }

    public T ConfigSection
    {
        get { return (T)Configuration.GetSection(_configSectionName); }
    }

    private System.Configuration.Configuration Configuration
    {
        get { return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); }
    }

    public delegate void ConfigChangedHandler();
    public event ConfigChangedHandler ConfigSectionChanged;

    protected void OnConfigSectionChanged()
    {
        if (ConfigSectionChanged != null)
            ConfigSectionChanged();
    }

    public void Dispose()
    {
        _fileWatcher.Changed -= FileWatcherOnChanged; 
        _fileWatcher.EnableRaisingEvents = false;
        _fileWatcher.Dispose();
    }
}
Published Wednesday, March 24, 2010 11:08 AM by dotjosh
Filed under: ,

Comments

# re: Monitoring settings in a configsection of your app.config for changes

Just a note.  FileSystemWatcher has problems with medium trust.  You might want to change this to use a cache dependency on the file (if its a web app.)  check out the ConfigFileChangeNotifier:  haacked.com/.../editable-routes.aspx

Wednesday, March 24, 2010 2:53 PM by Michael McDaniel

# re: Monitoring settings in a configsection of your app.config for changes

It's actually for a windows service/console app, which is why I said "app.config".  In an asp.net web app, I like the solution you linked if it's for a file that is NOT the web.config.. As you know (but just for reference of anyone coming upon this post), if your setting IS in your web.config, any change to that file causes the application to recycle and subsequently refresh your settings "automatically".

Wednesday, March 24, 2010 4:12 PM by dotjosh

# Twitter Trackbacks for Monitoring settings in a configsection of your app.config for changes - Josh Schwartzberg high-fives the CLR [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Monitoring settings in a configsection of your app.config for changes - Josh Schwartzberg high-fives the CLR         [asp.net]        on Topsy.com

Leave a Comment

(required) 
(required) 
(optional)
(required)