Setting up Development Configurations in Visual Studio

Working in a medium sized development team, we have found a source of frustration is when one developer checks in a change to the web.config that they’ve been using for testing that affects everyone else.

While looking at ways to stop this we discovered that you can have the web.config load settings in from other files.

ConfigSource

Some sections of the web.config allow you to add a ConfigSource attribute. for instance the connectionStrings section.  Before we would have several connectionStrings sections, one for each environment, but commented out.

Now, by adding the ConfigSource attribute, we can save the different connectionStrings sections to separate files, e.g. Dev, Dev2, etc., and simply change which one we point to. It’s much cleaner and more readable.

 <connectionStrings configSource="ConfigFiles\Dev_ConnectionStrings.config" />

AppSettings

AppSettings doesn’t allow you to use the ConfigSource, however, it does allow you to use the file attribute. The difference is that the file attribute brings in settings from another file that override the current settings.

<appSettings file="LocalSettings\DeveloperAppSettings.config">
   …
   …
</appSettings>

Also, the file is optional, so that if it doesn’t exist, the project will still run.

Example web.config extract

<connectionStrings configSource="ConfigFiles\DEV_ConnectionStrings.config" /> 
<appSettings file="LocalSettings\DeveloperAppSettings.config">
   <add key=”settingToOverride” value=”1”/> 
</appSettings>

ConnectionStrings.config

<connectionStrings>
  <add name="Database1Connection" connectionString="data source=Server1;initial catalog=Database1;user id=Database1User;password=password;" providerName="System.Data.SqlClient" />
  <add name="Database2Connection" connectionString="data source=Server2;initial catalog=Database2;user id=Database2User;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>

DeveloperAppSettings.Config

<appSettings>
   <add key=”settingToOverride” value=”2”/>
</appSettings>

Excluding the local files from TFS

First, the solution or project should have a “.tfignore” file created.

Example .tfignore file

###############################
# Ignore developer config files
\LocalSettings
# ignore logs
\logs
###############################
  • Check this file in.
  • Close and reopen the solution – This is important, as it seems to initialise the .tfIgnore.
  • Create the folder LocalSettings
  • Add the ConnectionStrings.config file
  • Add the DeveloperAppSettings.config file
    • Under Properties, set the Build Action to NONE
  • In Source Control Explorer, undo the changes to the DeveloperAppSettings.config file.

See also:

Advertisements

About John

Software developer, lately specialising in Full Stack Web Development using c#, ASP.Net WebAPI 2, and Angular.
This entry was posted in Configuration, Development and tagged , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s