Tutorial :Multiple applications sharing same configuration files



Question:

In my production environment I have several Web applications installed in the same machine. These applications access various web services and two SQL Server databases this means that my web.config files are very big and full of details. Besides this I also have windows services that consume the same web services and access different databases and, again, I have big app.config files with lots of details. The entire solution was developed using .NET 3.5 (C#).

My problem begins when one of my four development team wants to deploy a new version of some part of solution and all configuration files need to be changed. Because my QA environment have different machine names from the production environment I can’t do a simple copy of configuration files I need manually change each one. And, no matter how many checks my colleagues do (even using check lists), some config is left unchanged and some part of the solution stops.

Is there a way to create a unique config file for all applications? Or, at least, centralize the data base connection strings and web services URL in a file to be used by all applications?

P.S.: I can’t change the deployment team because they belongs to my customer :(


Solution:1

You can use the configSource attribute in the connectionStringssection to include an external connection strings file:

Application web.config

<configuration>    <connectionStrings configSource="connectionStrings.config" />  </configuration>  

connectionStrings.config

<connectionStrings>    <add name="connection1" connectionString="..." />  </connectionStrings>  

Note: as marc_s suggests in the comments, the configSource attribute can be used on any config section.


Solution:2

If I understand correctly you want to factor out some 'stable' settings

  1. The config system is hierarchical, you can store default settings in
    C:\Windows\Micorsoft.NET\<version>\Machine.config or Web.config
    That is a pretty heavy tool though. Make an error and most .NET apps will fail to start

  2. Use ConfiguratonManager class to get settings from a shared path. That may require changing (all) code that reads the settings though.


Solution:3

You can include external xml files into your web.config.

See this question

<!-- SomeProgram.exe.config -->  <configuration>    <connectionStrings configSource="externalConfig/connectionStrings.config"/>  </configuration>    <!-- externalConfig/connectionStrings.config -->  <connectionStrings>    <add name="conn" connectionString="blahblah" />  </connectionStrings>  


Solution:4

Here is another idea (actually how we solve our config problem). Maybe this fits your needs better.

Our config files are rather big too but the only thing that really changes are the configuration strings.

We have a file for each configuration which contains the specific configuration string (database for development, database for build, database for QA). The files looks like this:

<templates>    <template key="db" value="development server connection string"/>    <template .../>  </templates>  

These files are named after the environment they are used in (devel.xml, build.xml, qa.xml)

In the configuration files the connection strings are replaced by place holders (${db}).

We have build configurations for each environment (devel, build, qa). When building such a configuration a post build event replaces the place holders in the config files with the values stored in the template files. This generates the correct config files for each environment.

The idea is taken from Managing Multiple Configuration File Environments with Pre-Build Events but extended with the template replacement.


Solution:5

You could centralize parts of the configuration and dynamically load that file. See this codeproject article


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »