1

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?

Ravi
  • 326
  • 1
  • 4
  • 10
  • Ask yourself these questions first. **A:** What is my development technology for other projects besides this one project (Java - .Net – iOs Android – Many: Multiple Languages/OS usage). **B:** How many different End State Binaries or projects do you foresee generating in the next few years (5 or 500). **C:** Have I Already bought into an implied infrastructure such as AWS or Azure or Always on Perm). **D:** Is my source control choice flexible or does it imply a specific architecture technology Answer these questions first. – Sql Surfer Nov 03 '17 at 12:18

1 Answers1

1

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.

Matt
  • 25,467
  • 18
  • 120
  • 187
  • Thank you for the reply. This really helped. But this is limited to work with multiple web.config files. I also have multiple applicationSettings to work with. How do I load one based on an environment? – Ravi Nov 06 '17 at 12:26
  • If you have `applicationSettings` in your web project, they are also stored in your `Web.config` file. The difference is that applicationSettings deal with complex data types as well, not only with string values. So you have to use a different transform (`xdt:Transform="Replace"`) and replace not only one single entry but the entire setting. Let me know if you need more info, then I will update my answer. – Matt Nov 06 '17 at 12:41
  • Another thing I wanted to know "is it possible to have multiple settings.settings file in a project"? If yes, how do I make sure a specific setting file gets built? – Ravi Nov 07 '17 at 06:31
  • Yes - but only as a config transform. That means as soon as you publish one of the configurations you will get different settings if you have specified them in the config transform file. In the configuration combobox select the last entry, which is "Configuration manager...". There, click on "Active solution configuration" combobox and select "". Type in the name and you have a new solution config you can use for a different transform and publish profile. – Matt Nov 07 '17 at 09:27