Reload xml file if change
I have an XML file and I save some of my application settings in that.
I read this xml file and cache it in memory, If i change the setting i want to read the latest change from xml 开发者_如何学运维file but how can I know that, xml file is changed ?Im running this app in the web context (asp.net)
You can use a FileSystemWatcher class to monitor the file.
Alternately, you may want to consider savings settings in the web.config file, either in appSettings or a custom configuration section. Your web application will reload automatically when your configuration changes.
if you put your application settings in an app.config file you can force an update of the cached settings with:
//update app settings
ConfigurationManager.RefreshSection("appSettings");
The 'FileSystemWatcher' option has been answered, but you could also use the ASP.NET Cache with a 'file depenency'.
I'm assuming you don't want real-time updates being sent to the browser, you just want to use the new values server-side when some other code is running.
At app startup your app reads the file and inserts the settings object into the ASP.NET cache with a 'file dependency'. Each time through your code checks to see if the object is in the cache and if found it keeps using the same object. When the file changes, you guessed it, the cache will evict the old copy of your settings, which means you'll get null back from the cache. At this point your 'startup' code path runs again, reloads the file, and inserts the new setting object back into the cache (with the dependency).
The ASP.NET Cache is threadsafe, and as long as your deserialization doesn't cause any side-effects it is safe to read and insert without any locking or synchronization. (This means it is possible for two requests/threads to re-read the updated settings file at the same time, but this is usually OK, as long as the file is small.) IIRC the FileSystemWatcher solution is harder to get right and requires synchronization.
http://www.dotnetfunda.com/articles/article791-data-caching-with-file-dependency-in-aspnet-.aspx
Use FilesystemWatcher
public class MessageFileWatcher
{
public MessageFileWatcher(string Path, string FileName)
{
FileSystemWatcher Watcher = new FileSystemWatcher();
Watcher.Path = Path;
Watcher.Filter = FileName;
Watcher.NotifyFilter = NotifyFilters.LastWrite;
Watcher.Changed += new FileSystemEventHandler(OnChanged);
Watcher.EnableRaisingEvents = true;
}
private void OnChanged(object source, FileSystemEventArgs e)
{
// Do something here based on the change to the file
}
}
I don't know if it will work well in a webservice context (there are hits on the web about using it in that scenario), but you can use the FileSystemWatcher class.
There may be reasons not to do this, though. At least one team I have worked on had security reasons to avoid automatically executing code on changes to their config file. Instead, they opted to periodically reload the file, regardless of changes (every 30 seconds).
Depending on your requirements, you may also be able to store your settings in a database table, and avoid caching values in your code. This would allow you to take advantage of DB features (history, rollback, remote editing, permissions, taking advantage of any existing DB deploy/backup strategy you already have), and allow you to use that configuration in stored procedures, not just in your web service code.
精彩评论