Alex Rivera | Logout

Developer specific app.config/web.config files in Visual Studio

Asked 2010-09-14T09:46:49.027
98

We have several .NET projects where we store certain settings in configuration files.

Now each developer will have their own configuration files that differ a little (different connection strings to connect to local databases, 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 configuration when getting latest version from TFS.

How do you deal with situations like this? Or don't you have this problem at all?

Edit
Report

2 Answers

20

Here a solution for web.config files and Visual Studio 2010:

1) Edit manually your web application .csproj file to add an AfterBuild Target like this:

  <Project>
   ...
    <Target Name="AfterBuild">
      <Copy SourceFiles="web.config" DestinationFiles="obj\$(Configuration)\tempweb.config" />
      <TransformXml Source="obj\$(Configuration)\tempweb.config"
                  Transform="web.$(USERNAME).config"
                  Destination="obj\$(Configuration)\tempweb2.config" />
      <ReadLinesFromFile File="obj\$(Configuration)\tempweb2.config"><Output TaskParameter="Lines" ItemName="TransformedWebConfig"/></ReadLinesFromFile>
      <ReadLinesFromFile File="web.config"><Output TaskParameter="Lines" ItemName="UnTransformedWebConfig"/></ReadLinesFromFile>
      <Copy Condition=" @(UnTransformedWebConfig) != @(TransformedWebConfig) " SourceFiles="obj\$(Configuration)\tempweb2.config" DestinationFiles="web.config" OverwriteReadOnlyFiles="True" />
    </Target>
  </Project>

This target will transform the Web.config file corresponding to the current developer logged in - hence the $(USERNAME) variable -, with the corresponding file created in 1). It will replace the local Web.config only if the content has changed (to avoid restart) at each build, even if the local Web.config is source controlled, that's why there is the OverwriteReadOnlyFiles is set to True. This point in fact is arguable.

2) Create a file named Web.[developer windows login].config for each developer in the project. (for example in the following screenshots I have two developers named smo and smo2):

enter image description here

These files (1 per developer) can/should be source-controlled. They should not be marked as dependent on the main Web.config beca

answered 2012-01-23T14:51:40.670
0

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.

answered 2010-09-14T09:53:19.580

Your Answer