Silverlight Journal

December 31, 2009

Static object management with file watchers

Filed under: Uncategorized — Tags: , , — bobbsmooth @ 6:15 pm

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

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.