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.
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:
<appSettings file="settings.config" />
The settings.config file contains text similar to this:
<add key="DevelopmentMode" value="Live" />
<add key="AnotherSetting" value="42" />
Multiple keys can be used for storing other settings.
To retrieve data from the settings is done just as if they were stored in web.config. Fer example:
public string GetDevelopmentMode()
To update the data and persist the change, requires writing the new data manually to the settings file, and updating the cached setting:
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");
XmlNode node = xmldoc.SelectSingleNode("appSettings/add[@key = 'DevelopmentMode']");
XmlNode valueAttribute = node.Attributes.GetNamedItem("value");
if (valueAttribute != null)
valueAttribute.InnerXml = newDevelopmentMode;
//update the cached setting
This method allows for updating and persisting infrequently-changing settings without the web service having to restart.
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
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.