Ravi
Ravi

Reputation: 326

Build .Net Web application based on environment

There are 3 environments through which my .Net web application goes namely Development, Release and Production with each having their own config and project setting files.

Assuming that the setting and config files for different environments are in one system, I want to create a small script or an application where the developer just mentions the environment type and the related setting and config files get loaded and then the application builds.

Can anyone guide me on this?

Upvotes: 1

Views: 118

Answers (1)

Matt
Matt

Reputation: 27001

You can create config transforms and use them in publish profiles. For each configuration (Debug, Release, YourOwnConfig ...) there will be a file named by its configuration (Web.Debug.config, Web.Release.Config, Web.YourOwn.Config, ...)

The trick is that you have one complete config file, the original Web.Config, and the transforms just mention the differences to this file via XSLT transform syntax (once you create a new transform, there will be some examples in the file itself showing the syntax). For example, adding a transform for an appSettings key looks like:

<configuration>
    <appSettings>
        <add key="ClientSessionTimeout" value="100"
            xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
    </appSettings>
</configuration>

That example will replace an existing ClientSessionTimeout setting by the one specified (with value="100"). Notice how the xdt:locator specifies that the key attribute will be used to localize the setting, and xdt:Transform specifies that the attributes mentioned (here: value) will be set.

If you have applicationSettings, you need to replace the setting itself:

<applicationSettings>
    <WebApplication2.Properties.Settings>
        <setting name="Setting" serializeAs="String"
                                xdt:Transform="Replace" xdt:Locator="Match(key)">
            <value>Some value</value>
        </setting>
    </WebApplication2.Properties.Settings>
</applicationSettings>

The differences will be for example the data source settings, other environment specific settings such as URLs to web services etc.

To create those, select a configuration such as "Debug", then right-click on the Web.Config file and you will see a context menu item "Add config transform" - click it and the Web.Debug.Config transform file will be created underneath the Web.Config. Adapt it as mentioned before; copy the entire key or setting to the transform file, then add the appropriate xdt attributes as shown above.

Finally, you can use the "Publish" function (Right-Click on the web prroject to select it). A wizard opens where you can set up a publish profile. There you can mention a configuration - like "Debug", "Release", and the ones you've created earlier.

A file publish will put the files together needed to deploy the web project and additionally perform the transformation of the Web.Config by applying the appropriate transform file (e.g. Web.Release.Config). The published config will be named "Web.Config" and contains all changes.


For trouble-shooting, and to find out more about the topic, I recommend the following links:

Notice also the side-bar of Stack overflow showing more related links.

Upvotes: 1

Related Questions