Get a mop and wipe it up!


Updatable Config Settings for Web Services using C# 2.0

, posted: 22-Nov-2006 11:47

Visual Studio 2005 WinApps can use User Settings for read/write properties that are very easily persisted. Or Application Settings for read only data, such as connection strings.


Web Services cannot use User Settings. They can retrieve data from web.config, and it is not difficult to update it, for the current session only. But it is difficult to persist the changes between restarts. (Note that config files should not be used for frequently changing data). If the web.config file is changed, the web service is automatically restarted by IIS.


There are situations in which it is useful to be able to occasionally update settings programmatically. Below is my method for doing so.


Web.Config

Instead of storing data directly in web.config, add a file reference to a separate config file. I have chosen to call it "settings.config". The web.config contains a section such as:


<configuration>
  <appSettings file="settings.config" />
</configuration>

Settings.Config


The settings.config file contains text similar to this:


<appSettings>
  <add key="DevelopmentMode" value="Live" />
  <add key="AnotherSetting" value="42" />
</appSettings>

Multiple keys can be used for storing other settings.


Retrieve Setting

To retrieve data from the settings is done just as if they were stored in web.config. Fer example:

        [WebMethod]
        public string GetDevelopmentMode()
        {
            return System.Configuration.ConfigurationManager.AppSettings.Get("DevelopmentMode");
        }


Save Setting

To update the data and persist the change, requires writing the new data manually to the settings file, and updating the cached setting:

        [WebMethod]
        public void SetDevelopmentMode(string newDevelopmentMode)
        {
            //update the config file
            XmlDocument xmldoc = new XmlDocument();
            string baseDir = System.Web.HttpRuntime.AppDomainAppPath;
            string configPath = Path.Combine(baseDir, "Settings.config");
            xmldoc.Load(configPath);
            XmlNode node = xmldoc.SelectSingleNode("appSettings/add[@key = 'DevelopmentMode']");
            XmlNode valueAttribute = node.Attributes.GetNamedItem("value");
            if (valueAttribute != null)
            {
                valueAttribute.InnerXml = newDevelopmentMode;
                xmldoc.Save(configPath);
            }

            //update the cached setting
            System.Configuration.ConfigurationManager.AppSettings.Set("DevelopmentMode", newDevelopmentMode);
        }

Summary

This method allows for updating and persisting infrequently-changing settings without the web service having to restart.



Tag(s):       


Other related posts:
How to get your Windows Azure hosted WordPress contact form working
NuPeek - a local NuGet and Symbol Server for .NET
Caliburn.Micro for Newbies Who Want to Write Clean WP7 Apps - Part 1: How to Configure a New Project With Folders








Author's note by dmw, on 15-Sep-2007 12:39

There's an excellent and comprehensive series of articles on .NET 2.0 Configuration at The Code Project.


Comment by rana, on 25-Oct-2009 21:56

nice article, helped me. Thanks


Add a comment

Please note: comments that are inappropriate or promotional in nature will be deleted. E-mail addresses are not displayed, but you must enter a valid e-mail address to confirm your comments.

Are you a registered Geekzone user? Login to have the fields below automatically filled in for you and to enable links in comments. If you have (or qualify to have) a Geekzone Blog then your comment will be automatically confirmed and placed in the moderation queue for the blog owner's approval.

Your name:

Your e-mail:

Your webpage:

dmw's profile

David White
Auckland
New Zealand


Goon fan, .NET developer, contrarian seeker of truth