Reputation: 33471
I need to switch among 3 different environments when developing my web app - Development, UAT, and Prod. I have different database connections in my configuration files for all 3. I have seen switching these settings done manually by changing all references and then rebuilding the solution, and also done with preprocessor directives. Is there an easy way to do this based on some variable so that the configuration doesn't have to be revised when deploying to a new environment every time?
Upvotes: 10
Views: 7846
Reputation: 3417
Scott Hanselman has suggested one way to do this:
http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
Upvotes: 2
Reputation: 6795
You can always use NAnt + NAnt.Contrib to modify the web.config during the build. NAnt has xmlpeek and xmlpoke tasks which allow you to update xml files.
e.g.
<xmlpoke file="${dist.dir}/Web.config" xpath="/configuration/applicationSettings/MyProj.Web.Properties.Settings/setting[@name = 'MyProj_Web_Service']/value" value="http://${AppServer}/Service.asmx" />
Upvotes: 2
Reputation: 1525
I have adopted the Jean Paul Boodhoo Method of changing configurations. The general idea is to have one or more TOKENIZED configuration TEMPLATE files instead of the configuration files themselves. Then you have a build script task that replaces the tokens with values from a SINGLE local properties file. This properties file contains all differences in configuration and is unique per working copy.
This system has worked great for me and once initially setup is a breeze to manage environment changes.
Upvotes: 1
Reputation: 25099
I'm a big fan of using MSBuild, in particular the MSBuild Community Tasks (http://msbuildtasks.tigris.org/) and there is an XSLT task to transform the web.config with the appropriate connection string settings, etc.
I keep these tasks handy:
<Target Name="Configs">
<Xslt RootTag="" Inputs="web.config" Output="Web.$(COMPUTERNAME).config" Xsl="web.config.$(COMPUTERNAME).xslt" Condition="Exists('web.config.$(COMPUTERNAME).xslt')" />
Obviously this isn't 100% what you're after, it's so each dev can have their own web.config.
But there's no reason you couldn't use the above principle to have multiple build configurations which applies the right XSLT.
My XSLT looks like this:
<?xml version="1.0" encoding="utf-8"?>
<!-- Dev -->
<xsl:template match="/configuration/connectionStrings/add[@name='MyConnectionString']/@connectionString">
<xsl:attribute name="connectionString">Data Source=MyServer;Initial Catalog=MyBD;User ID=user;password=pwd</xsl:attribute>
</xsl:template>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
Upvotes: 3
Reputation: 28325
To me it seems that you can benefit from the Visual Studio 2005 Web Deployment Projects.
With that, you can tell it to update/modify sections of your web.config file depending on the build configuration.
Take a look at this blog entry from Scott Gu for a quick overview/sample.
Upvotes: 11