Click here to Skip to main content
15,868,123 members
Articles / Programming Languages / C#
Article

ConfigManager.Net - App.config and Web.config helper utility

Rate me:
Please Sign up or sign in to vote.
4.38/5 (11 votes)
21 Jun 2008CPOL4 min read 76.2K   1.2K   61   8
Utility to aid in the management of app.config and web.config files in a team development environment.

Introduction

In developing ConfigManager.Net, I set out to address the following problems:

  • While many of the items in the app.config\web.config file are generic to all team members, some settings, especially in the appSettings and connectionStrings sections, may need to be different.
  • The configuration sections of referenced assemblies is not automatically merged into the configuration file of the main application.
  • Although it is possible to store the appSettings and connectionStrings sections in external files, the same cannot be done for other sections (e.g., system.ServiceModel).

ConfigManager.Net solves these problems by executing embedded instructions in the app.config. These instructions are stored in the file as XML comments, and executed in place.

Alternatives

Other projects have also sought to address the problem of merging configuration files, including XmlConfigMerge. The most common methods of allowing for different settings among team members are:

Check-in the settings most appropriate for most team members

Developers who need to use different settings must then use a writable copy of the file. The main drawback of this approach is the difficulty of keeping the files in sync. Additionally, every time developers with the writable file gets the latest set of files from the source control, they will be prompted each time to replace the file.

Using the external file features of the app.config\web.config file

The appSettings and connectionStrings sections can include references to external files.

XML
<!-- Reference appSetting in the file localAppSettings.config -->
<appSettings file="localAppSettings.config" />

<!-- Reference appSetting in the file localConnectionStrings.config -->
<connectionStrings configSource="localConnectionStrings.config" />

This approach results in more configuration files that need to be managed and synchronized. It is also limited to the above mentioned sections of the app.config, sections like system.ServiceModel cannot reference external files in the same way.

Using ConfigManager.Net

Valid instructions to ConfigManager.Net must begin with the #ConfigManager.Net# keyword, followed by the instruction type (e.g., ExternalSection), and finally, parameters in the form paramName="paramValue". Properties can be used in these parameters, these properties are discussed later.

All instructions support the special parameter condition. This parameter does a string compare, and only if it evaluates to true is the instruction executed. It takes the form condition="value1==value2" or condition="value1!=value2". For example, to only execute the instruction on the the computer "MyComputer", the condition would be condition="$(Environment.MachineName)==MyComputer".

To run the utility, call:

ConfigManager.exe app.config

The best place to do this is either in the post built event of the project or as part of a build script.

Post Build Event

Instructions

EmbeddedSection instruction

XML can be specified within the comments of this instruction. This XML will be added to the configuration file if the condition parameter evaluates to true.

XML
<connectionStrings>
    <!-- You can embed a section based on a condition, such as the user name. -->
    <!-- #ConfigManager.Net# EmbeddedSection condition="$(Environment.UserName)==user"
    <add name="MyConnectionString"
         connectionString="Data Source=localhost;
                           Initial Catalog=MyDatabase;Integrated Security=True"
         providerName="System.Data.SqlClient" />
    -->
</connectionStrings>
ParameterMandatoryDescription
conditionnoIf this parameter evaluates to true, the instruction will be executed.

ExternalSection instruction

This instruction copies the contents (or part of the contents) of an external file into the configuration file. If no value is supplied for the xpath parameter, the entire contents of the file are copied. The file must contain valid XML or XML fragments.

XML
<!--Copy the appSettings from another app.config file into the current file. -->
<!-- #ConfigManager.Net# ExternalSection
   source="alternative.config"
   xpath="/configuration/applicationSettings/Alternative.Properties.Settings" -->
ParameterMandatoryDescription
conditionnoIf this parameter evaluates to true, the instruction will be executed.
sourceyesThe path to the external file.
xpathnoThe XPath expression for the fragment of the file to be copied.

OpenSection instruction

An OpenSection instruction allows a section of the XML file to be kept or removed from the configuration file, based on the condition parameter. The scope of this instruction is from the OpenSection to the corresponding EndOpenSection. If the condition parameter evaluate to false, the XML fragment surrounded by the instruction will be removed from the file. Although the condition parameter is not mandatory, if it is not provided, the instruction will evaluate to true and the XML fragment will not be removed from the file.

XML
<connectionStrings>
    <!-- #ConfigManager.Net# OpenSection condition="$(Environment.UserName)!=fergal" -->
    <!-- You you can remove a section based on a condition. -->
    <add name="RemoveConnectionString"
         connectionString="Data Source=localhost;
                           Initial Catalog=MyDatabase;Integrated Security=True"
         providerName="System.Data.SqlClient" />
    <!-- #ConfigManager.Net# EndOpenSection -->
</connectionStrings>
ParameterMandatoryDescription
conditionnoIf this parameter evaluates to true, the instruction will be executed.

SetValue instruction

Changes a value within the configuration file. This can be the inner text of a node of the value of an attribute.

XML
<!-- Change the value of the UserName application setting. -->
<!-- #ConfigManager.Net# SetValue
   xpath="/configuration/applicationSettings/ConfigManagerDemo.
          Properties.Settings/setting[@name='UserName']/value"
   value="newUser"   -->
ParameterMandatoryDescription
conditionnoIf this parameter evaluates to true, the instruction will be executed.
xpathyesThe XPath address of the value to be changed.
valueyesThe new value.

Properties

A property can be used anywhere in an instruction. This is done by placing the property name between "$(" and ")" in the instruction. ConfigManager.Net has the following built-in instructions:

Environment.CurrentDirectoryThe fully qualified path of the current working directory.
Environment.MachineNameThe NetBIOS name of this local computer.
Environment.OSVersionThe current platform identifier and version number.
Environment.SystemDirectoryThe fully qualified path of the system directory.
Environment.UserDomainNameThe network domain name associated with the current user.
Environment.UserNameThe user name of the person who is currently logged on to the Windows operating system.

In addition to these properties, all environment variables are exposed as properties. They can be accessed as Environment.Variable.xxx. To get the full list of properties in the ConfigManager.Net, run: ConfigManager --properties.

Command line properties

Additional properties can be passed into the ConfigManager.Net on the command line:

ConfigManager.exe app.config prop1="value1" prop2="value2" 

Changes

  • 2 June 2008 - Changed the utility to produce UTF-8 instead of UTF-16 output.
  • 22 June 2008 - Fixed bugs found by oupoi.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCCNet XmlConfig Plugin Pin
void leenux();16-Apr-11 7:42
void leenux();16-Apr-11 7:42 
GeneralDid you hear something about DotNet.Helper Project Pin
void leenux();15-Apr-11 2:11
void leenux();15-Apr-11 2:11 
GeneralLogin.aspx Pin
Ajay Kale New27-Sep-10 0:04
Ajay Kale New27-Sep-10 0:04 
GeneralRe automatic redirection... Pin
Ajay Kale New10-Sep-10 4:20
Ajay Kale New10-Sep-10 4:20 
Hi

I have a query regarding redirection to Login page, which I am not being able to trace out.

When I click on any of the tab in my application (which internally loads a new aspx page) sometimes it
is redirected to Login.aspx, which is unexpected. Couldnot debug why this is happening even by javascript alerts and C# debugging statements.

Below is the view source piece from Login.aspx which is unexpectedly displayed,
just for clue
<form name="Form1" method="post" action="login.aspx?ReturnUrl=%2fAppName%2fClickedPage.aspx" id="Form1">



- we have put the logger statements and put alerts everywhere, but no trace...and can't say for sure whether request goes to server or not as no logg is availavle when login.aspx is displayed.

- <authorization>
<deny users="?"/>

</authorization>

-suddenly while traversing in the application it redirects to login.aspx including return url as stated earlier and also if we keep the application idle for 5-10 mins and clicks somewhere, still redirects....

- we have also AjaxPro.dll for async calls.

- when the login.aspx page is not closed then, below logger statements are seen after 5 mins
-public Global()
{
_log4netLogger.Debug("Global:Global");
InitializeComponent();
}
- catch of Session_End (because System.Web.HttpContext.Current.Application["userPool"]; is null)
- Application_End


Can you please help me...?

- Ajay K
Generalexternal application settings for windows app.config Pin
smh110-Mar-09 8:49
smh110-Mar-09 8:49 
GeneralFeature request: create a new file, rather than overwritting the file. Pin
bobqwe30-Jul-08 6:48
bobqwe30-Jul-08 6:48 
GeneralThanks and bugs found Pin
oupoi5-Jun-08 1:17
oupoi5-Jun-08 1:17 
GeneralCool Pin
leppie31-May-08 23:07
leppie31-May-08 23:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.