views:

115

answers:

6

Hi, we have several .net projects where we store certain settings in config files. Now each developer will have their own config files that differ a little (different connection strings to connect to local db, different wcf endpoints etc.)

At the moment we tend to check out app/web.config files and modify them to suit our needs. This leads to many problems since from time to time someone will check in their own settings or loose custom config when getting latest version from tfs.

My question is: how do you deal with situations like this? or you don't have this problem at all?

A: 

We are using machine.config to avoid having differences in web.config between the environments.

Darin Dimitrov
would rather want to avoid having to change machine.config
twarz01
A: 

How about ignoring the file, so it never gets checked in? I've run into a similar issue and have added web.config to the ignore list in Subversion.

In TFS though, it's a little bit harder, see this post on how to do it.

Marko
A: 

One way you could cope is to have a tokenised system and use a rake script to change the values.

A more rudimentary method could be to have a link to an AppSettings.config file for all AppSettings in your web.config (similar with connections) i.e.

<appSettings configSource="_configs/AppSettings.config" />

Then have a folder with each of your developers have a version in a sub folder (i.e. /_configs/dave/). Then when a developer is working on their own code they copy from the sub folder to the root of the linked folder.

You will need to make sure you communicate changes to these files (unless you tokenise). If you keep the AppSettings.config file out of source control and only check in the devs individual folders (all of them) then they will be forced to copy the correct one.

I prefer tokenisation but can be tougher to get up and running if this is only meant to be a quick fix.

ArtificialGold
A: 

In your Web.config use source from other files

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

Keep the web.config in version control and don«t do it for ConnectionStrings.config. Now all developers have one file for the connection string.

You can do this for all the settings that are local dependant.

fampinheiro
+1  A: 

Ignore the files and have a Commom_Web.Config and Common_App.Config. Using a continuous integration build server with build tasks which rename these two to normal names so that the build server can do its job.

Yoann
this needs to be done on dev machines, before it goes to build server
twarz01
No this renaming should happen on the build server. Common config files being stored on the subversion, they are independent of a specific developper.Whereas every developper runs its personnal config files to develop on his machine, and do not care of submitting it because it is not part of the subversion.
Yoann
A: 

We use a system that combines several of the existing answers on this page, plus draws on this suggestion by Scott Hanselman.

In short, what we did was to have a common app.config / web.config, and to have most of the specific settings in individual files, as suggested by other answers here. e.g. for our SMTP settings, the app.config contains

<system.net>
  <mailSettings>
    <smtp configSource="config\smtp.config" />
  </mailSettings>
</system.net>

This file is in source control. However, the individual files, like this, are not:

<?xml version="1.0" encoding="utf-8" ?>
<smtp deliveryMethod="Network">
  <network host="127.0.0.1" port="25" defaultCredentials="false" password="" userName ="" />
</smtp>

That's not quite where the story ends though. What about new developers, or a fresh source installation? The bulk of the configuration is no longer in source control, and it's a pain to manually build all of the .config files they need. I prefer to have source that will at least compile right out of the box.

Therefore we do keep a version of the .config files in source control, named .config.default files. A fresh source tree therefore looks like this:

alt text

Still, not really any use to the developer, since to Visual Studio they're just meaningless text files. Hence the batch file, copy_default_config.bat, takes care of creating an initial set of .config files from the .config.default files:

@echo off
@REM Makes copies of all .default files without the .default extension, only if it doesn't already exist.
for %%f in (*.default) do (
    if not exist %%~nf (echo Copying %%~nf.default to %%~nf & copy %%~nf.default %%~nf /y)
)
echo Done.

The script is safely re-runnable, in that developers who already have their .config files will not have them overwritten. Therefore, one could conceivably run this batch file as a pre-build event. The values in the .default files may not be exactly correct for a new install, but they're a reasonable starting point.

Ultimately what each developer ends up with is a folder of config files that looks something like this:

alt text

It may seem a little convoluted, but it's definitely preferable to the hassle of developers stepping on each other's toes.

Gavin Schultz-Ohkubo