all groups > dotnet clr > april 2006 >
You're in the

dotnet clr

group:

Getting AppSettingsReader to use app.config file changes at runtime



Getting AppSettingsReader to use app.config file changes at runtime Matt Adamson
4/25/2006 12:12:15 PM
dotnet clr: Guys,

When writing unit tests for our assemblies we'd like to change configuration
values at runtime for some tests i.e. to test all code paths are executed.
e.g. one method would create a test with a value set to true and the next
test method would set it to false.

I can set the values in the config file easy enough using the following
approach

1) Get access to the app.config file full path from the process using
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

2) Load the file into an XmlDocument

3) Extract the relevant node using code such as XmlNode node =
xmlDocument.SelectSingleNode(@"/configuration/appSettings/add[@key='" +
configurationName + "']");

4) Set the value of the "value" attribute using
node.Attributes.GetNamedItem("value").Value = configurationValue;


However is there any possible way to get the AppSettingsReader class to
reflect the changes at runtime. i.e. so you can execute code such as

AppSettingsReader appSettingsReader = new
System.Configuration.AppSettingsReader();
Boolean value = appSettingsReader.GetValue(key, typeof(Boolean));

and this would reflect the changes.

Any help appreciated

Cheers

Matt






Re: Getting AppSettingsReader to use app.config file changes at runtime hB
4/30/2006 10:00:26 PM
Better Implement your own Configuration Class. (Config App Block).
Like we normally do as:
class ConfigurationManager : Which reads the config values from one of
the config like just like the app.config and loads all data in the
Class ConfigManager into its properties.

This class can either load data from any xml/txt config file from any
path or registry or any location.
And we have more control and flexibility.

To implement dynamic runtime file changes, for filesystem based file,
implement filesystem watcher in this configmanager class, and have this
class a Load() function which fills up all the properties, and upon
detecting changes just call the Load()

---
hB
Re: Getting AppSettingsReader to use app.config file changes at runtime Matt Adamson
5/2/2006 12:00:00 AM
Thanks for this although I'm aware of a few alternative solutions I really
wanted to use the existing framework infrastructure for this.

The reason being is I want to avoid changing a lot of existing code just to
get our unit tests working well.

[quoted text, click to view]

AddThis Social Bookmark Button