tags:

views:

81

answers:

5

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)

A: 

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.

Merlyn Morgan-Graham
+3  A: 

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.

John Bledsoe
I haven't looked at this since .NET 2.0, but I believe any changes to web.config will cause an appdomain reload, which might cause a noticeable pause in the web app when compared to the original 'seperate file' solution.
crtracy
Indeed, changing web.config will reload the app domain. This could be a problem if your settings change frequently, but not if they change only rarely.
John Bledsoe
I've encountered consistency problems with the FileSystemWatcher class as well. Those may have been in .NET 1.1, but they've made me skittish of trusting the class completely.
John Bledsoe
A: 

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");
BrokenGlass
A: 

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
   }
}
Vinay B R
+1  A: 

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

crtracy
does FileSystemWatcher works in web context which is stateless ?
Ehsan