Azure App Services Specify Web Config Per Slot or App Service
Asked Answered
S

2

1

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:

  • Web.config
  • Web.Test.config
  • Web.Stage.config
  • Web.Prod.config

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

Sporogony answered 9/8, 2022 at 1:14 Comment(1)
Microsoft documented its thoughts, learn.microsoft.com/en-us/azure/app-service/…Angelicangelica
S
1

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.

Sporogony answered 11/8, 2022 at 19:36 Comment(0)
A
0

@user25794: Your solution helped me a lot.

I would like only to add a couple more things that other observers may need. In case you need to know how to actually edit the Web.Stage.config file you'll possibly need the following references: Theoretical Microsoft reference on how to edit transformation configuration files, Some more practical examples of XDT transformations, StackOverflow reference to another example.

And here is my example of a working transforming web.Staging.config file (my environment is called Staging):

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

    <system.webServer xdt:Transform="Insert">
        <httpProtocol>
            <customHeaders>
                <add name="X-Frame-Options" value="SAMEORIGIN" />
                <remove name="x-powered-by"/>
                <remove name="Server"/>
            </customHeaders>
        </httpProtocol>
    </system.webServer>

</configuration>

This example adds some security measures to a website:

  1. Adds the X-Frame-Options custom header to prevent your site to be opened through iFrame.
  2. Removes the X-Powered-By custom header to prevent observing this information by a potential hacker.
  3. Removes the Server custom header to prevent observing this information by a potential hacker.
Acerb answered 8/8 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.