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.
I find having several config files for each environment works well. ie:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With