Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set up solution configuration specific config files?

I have a web service that needs different settings for different environments (debug, test, prod). What's the easiest way to setup separate config files for these different environments? Everything I find on the web tells me how to use configuration manager to retrieve settings, but not how to find particular settings based on the current build configuration.

like image 262
James Avatar asked Jan 05 '09 21:01

James


2 Answers

I find having several config files for each environment works well. ie:

  • config\local.endpoints.xml
  • config\ dev.endpoints.xml
  • config\ test.endpoints.xml
  • config\ staging.endpoints.xml
  • config\ prod.endpoints.xml

I then link to a "master" version of this using the built in configSource attribute within the web.config or app.config such as

<appSettings configSource="config\endpoints.xml"/>

I would then use the build process or deploy process to copy the the correct configuration for the environment down to the name that the web.config is expecting.

Each environment is clearly labelled and controlled, without the need of messy placeholders.

like image 90
Xian Avatar answered Oct 19 '22 16:10

Xian


One way would be to maintain 3 different configuration files and choose them via MSBuild when deploying.

    <Choose>
        <When Condition="$(BuildEnvironment) == 'debug'">
            <PropertyGroup>
                <ConfigFile>debug.config</ConfigFile>
            </PropertyGroup>
        </When>
        <When Condition="$(BuildEnvironment) == 'test'">
            <PropertyGroup>
                <ConfigFile>test.config</ConfigFile>
            </PropertyGroup>
        </When>
        <When Condition="$(BuildEnvironment) == 'prod'">
            <PropertyGroup>
                <ConfigFile>prod.config</ConfigFile>
            </PropertyGroup>
        </When>
    </Choose>

By utilizing an MSBuild task you can rename and push the specific configuration file to the proper location.

Still somewhat cumbersome, this has the added advantage of moving towards a one step build.

like image 31
Gavin Miller Avatar answered Oct 19 '22 16:10

Gavin Miller