.netazureiis

Azure App Services Specify Web Config Per Slot or App Service


I'm new to Azure and setting up Microsoft Hosting so hoping someone can shed some light on environment specific Web.config files.

I've inherited a .NET web application which has different Web.config files per environment.

For example:

All of the config files contain environement specific configurations.

Previously the app was hosted using Azure's "App Services" and we're looking to do the same.

I'm aware that you can override values in Web.config using "App Settings" under the "App Services" configuration, however, is there a way to simply switch out the Web.config file used for each environment?

I imagine ideally, there wouldn't be seperate Web.config files and all values would be set via "App Settings" preventing the need to push environment specific values to the repo, still it'd be nice to know how to switch out the Web.config files / environment using Azure App Services.

If anyone could point me in the right direction It would be much appreciated. Wouldn't surpise me if I'm missing something simple here.

Thanks


Solution

  • OK ended up figuring this one out, will post a couple of solutions here incase anyone else is trying to do similar.

    Firstly, both solutions use Azure Deploy Pipelines (there's probably other deployment methods that would work in a similar way, for now we'll focus on Azure Release Pipelines)

    1. Use "XML variable substitution"

    The way this works is that when a release pipeline deploys the application to Azure App Service, the Web.config file variables are overridden by that defined in an environment specific Web.config file.

    For example, let's say we are deploying to stage, steps are roughtly as follows:

    1. Create a transofrmation configuration file named Web.Stage.config

    2. Ensure the name of the deploy stage is that of the environment, in this case, "Stage"

    3. In the "Azure App Service Deploy" task, make sure "XML Transformation" is checked.

    4. When deployed, the variables defined in Web.Stage.config will replace those in Web.config and deploy you application with an environment specific Web.config.

    5. If transformation fails just check the logs and debug as you go. It should work if you see a line simlar to this in your logs:

      Transform  file: 'D:\a\_temp\temp_web_package_6283924812067563\Content\D_C\a\1\s\XXX.Backend\src\XXX.Web.Cms\obj\Release\Package\PackageTmp\Web.Stage.config'
      Executing Insert (transform line 4, 60).
      

    For more info see - https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/transforms-variable-substitution

    2. Hack in a Post Deployment Script

    Like most IT tasks, there's a right and wrong way to do things, this solution is no way near as clean as "XML variable substitution", however would still work. Again we'll use Azure Release Pipelines, this time specifying a "Post Deployment Action" of type "Inline Script".

    Then, add an inline script to delete Web.config and move your environment specific Web.config to the original location of Web.config

    I would not suggest this approach, use "XML variable substitution" all the way as it is a cleaner approach.

    So in short, use a Azure Release Pipeline and "XML variable substitution" in order to use environment specific Web.config files.