Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 604
twarz01 Avatar asked Sep 14 '10 09:09

twarz01


People also ask

How do I find app config in Visual Studio?

In Solution Explorer, right-click the project node, and then select Add > New Item. The Add New Item dialog box appears. Expand Installed > Visual C# Items. In the middle pane, select the Application Configuration File template.

Where is the web config file in Visual Studio?

config file is located in the %SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\ folder. The default settings that are contained in the Machine.

Is app config the same as web config?

Web. Config is used for asp.net web projects / web services. App. Config is used for Windows Forms, Windows Services, Console Apps and WPF applications.


2 Answers

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. Does the same recursively through all child folders. for /r %%f in (*.default) do (     if not exist "%%~pnf" (echo Copying %%~pnf.default to %%~pnf & copy "%%f" "%%~pnf" /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.

like image 105
Gavin Avatar answered Oct 15 '22 13:10

Gavin


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.

like image 26
Filipe Pinheiro Avatar answered Oct 15 '22 11:10

Filipe Pinheiro