I wanted to have some objects remain in memory for use by some WCF services but still be able to refresh them without restarting the service. Basically, when certain files change, I wanted the static objects to be flushed and then re-initialized with the new values in the files.
private static System.IO.FileSystemWatcher _configWatcher = null;
private static void StartWatcher()
{
if (_configWatcher == null)
{
//set the path to a directory that I configured in the web.config file.
string path = HttpContext.Current.Server.MapPath(WebConfigurationManager.AppSettings["CONFIGURATION_DIRECTORY"]);
//initialize the watcher to watch any xml file in the directory identified by the path
_configWatcher = new System.IO.FileSystemWatcher()
{
Path=path,
EnableRaisingEvents=true,
NotifyFilter=System.IO.NotifyFilters.LastWrite,
Filter="*.xml"
};
//handle the changed event for when files are modified and the error event
_configWatcher.Changed += new System.IO.FileSystemEventHandler(_watcher_Changed);
_configWatcher.Error += new System.IO.ErrorEventHandler(_watcher_Error);
}
}
static void _watcher_Error(object sender, System.IO.ErrorEventArgs e)
{
//log error
}
public static void _watcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
//reinitialized dependent static objects
}
Advertisement