views:

136

answers:

3

Currently I can easily setup Web.config transform based on build configuration, e.g. use connectionString=server;.. for Debug and connectionString=./SQLExpress;.. for Release.

But is it possible to do some Web.config transformation basing on web publish profile? I.e. use connectionString=server1;.. for profile Server1 and connectionString=server2;.. for Server2 ?

A: 

I believe that the publishing profiles are independent of the build profiles, which is a bit of a shame, as you could easily accidentally deploy a debug configuration to your production servers.

However, if you're using MSDeploy, there are ways to modify the web.config in there. See MSDeploy - Changing Connection string parameter after deploying the package for more details.

Zhaph - Ben Duguid
A: 

There could be a slightly different way to do this.

On your production servers create a dummy entry, for customdb, in the c:\windows\system32\drivers\etc\hosts file on each of the production machines. Each one pointing to the database that you want that machine to use. Then you only have to point to the connectionString=customdb; for all your production servers.

Only downside of this would be that you would need access to the hosts file and it would require you to use a db.

Hope this helps

Jonathan Stanton
Interesting. Yet, another downside is that you cannot (easily) version-control the hosts file, as it will contain machine-specific settings and that it adds complexity to the deployment process, requiring write access to system paths you should normally avoid.
Abel
+3  A: 

We keep all machine/profile specific configuration in separate config files, then use configSource to include them like so...

    <connectionStrings configSource="cstrings.config"/>

This way Web.config is the same and doesn't require any transformations. We do this for connection strings, smtp settings and app settings.

We version control Web.config and "machine specific" files such as cstrings.config.production, cstrings.config.staging, etc.

Once you have this structure it's easy to generate images for different profiles. We have deployment scripts on each machine that read an environment variable and deploy appropriately. For example, the staging server build script copies cstrings.config.staging to cstrings.config, etc.

Rob
How do you run your deployment scripts to link the current profile and a appropriate connection string?
abatishchev
@abatishcev: Our build server has targets for test,staging and production. A clean checkout (svn export actually) gets made. Build scripts "rename ${bin}/cstrings.config.${Destination} ${bin}/cstrings.config" then we run unit tests, zip and ftp deploy to the destination machine. Each destination has cstrings, smtp and appsettings config files in version control. e.g., cstrings.config.staging, smtp.config.staging, appsettings.config.staging.FYI: You can leave cstrings.config on target machines as a read-only file if you're very security conscious. It's not a big deal in our environment.
Rob