Background: I'm using a Timer to do some work periodically within a Windows Service. I want the Timer to be configurable at run-time. The only thing I managed to do is configure it at startup.
My solution: I'm using app.config to configure start time and period of timer:
<appSettings>
<add key="StartTime" value="14:40:00"/>
<add key="Period" value="24:00:00"/>
</appSettings>
I'm using a FileSystemWatcher to notify of File Writes on the config file (will be AppName.exe.config)
public ConfigWatcher(params object[] args)
{
configurationChangedListeners = new List<INotifyConfigurationChanged>();
string assemblyDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
NotifyFilters notifyFilters = NotifyFilters.LastWrite;
_fileSystemWatcher = new FileSystemWatcher()
{
Path = assemblyDirectory,
NotifyFilter = notifyFilters,
Filter = "*.config"
};
_fileSystemWatcher.Changed += OnChanged;
_fileSystemWatcher.EnableRaisingEvents = true;
if (args != null)
{
foreach (var arg in args)
{
AddListener(arg);
}
}
}
private void OnChanged(object source, System.IO.FileSystemEventArgs e)
{
try
{
_fileSystemWatcher.EnableRaisingEvents = false;
ConfigurationManager.RefreshSection("appSettings");
foreach (var listener in configurationChangedListeners)
{
listener.NotifyConfigurationChanged();
}
}
finally
{
_fileSystemWatcher.EnableRaisingEvents = true;
}
}
Lastly, each listener is getting its configuration like so:
public void NotifyConfigurationChanged()
{
string strKeyName = "StartTime";
string startTime = ConfigurationManager.AppSettings[strKeyName];
// ...
}
And the problem: - When I edit the file, the File Watcher triggers the Event but when I try to get the new AppSettings I'm reading the OLD values (from when the service started)
The weird thing is, at some point this setup worked, and then it didn't (without changing code as far as I can tell).
Any help / suggestions are much appreciated.