views:

205

answers:

4

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 :(

A: 

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

Bernard Vander Beken
+1  A: 

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>
alexander.egger
Alexander, thank you for your answer but it does not fill all my needs. ConfigSource must refer to a file in the same directory or in a subdirectory of the configuration file. So, with your approach I will need to create new configs instead of reducing the number of them.
GRGodoi
+2  A: 

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.

Mehmet Ergut
this actually works for **any** configuration section - not just connectionStrings.
marc_s
yes of course, edited to include that, thanks.
Mehmet Ergut
+1  A: 

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.

alexander.egger