0
votes

I have an ASP.NET (4.7.2) app that successfully runs and starts on my machine. This app has a web.config file that contains the following:

Web.config

<appSettings>
  <add key="username" value="[email protected]" />
  <add key="port" value="25" />
</appSettings>

These are the configuration values I want to use while working. However, when I deploy the app to my Azure App Service for test purposes, I want to change the port value. For that reason, I've added a config transform named "Web.Test.config" with the following:

Web.Test.config

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="port" value="58" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
  </appSettings>
</configuration>

Unfortunately, it doesn't seem like the transform is happening. I've ready that the transforms only happen during "publishing". At this time, I'm deploying via an Azure DevOps Pipeline that includes the following tasks:

NuGet

Restores the packages based on the .sln file

MSBuild

Builds the .csproj that defines my ASP.NET app. The "Configuration" property is set to "Test".

Azure App Service Deploy

Attempts to deploy the ASP.NET app as a "Web App on Windows" to my deployment slot. The "Package or folder" is set to MyAspNetApp. The

While this build pipeline successfully runs, the configuration transforms do not seem to be taking effect. How do I do a "publish" via an Azure DevOps Build Pipeline to an Azure App Service so that my config transformations will be generated.

2

2 Answers

1
votes

For App.Config or Web.Config (XML) file there are two options.

  1. File Transform task suggested by @Jabberwocky in the comment.
  2. Variable Substitution as per below image in the release pipeline.

Sample XML variable substitution

For modern .NET apps where we use json configuration, variable substitution is the only way. Even though File Transform task promises to work for both it is not clear as per this thread

For JSON variables, you have to enter the full path hierarchy like this

Sample JSON variable substitution

0
votes

How do I do a "publish" via an Azure DevOps Build Pipeline to an Azure App Service so that my config transformations will be generated.

First, we need to make sure the web.*.config files are included in the build output and the file is transformed correctly. Check it locally first.

Besides, the following steps should help:

  • Remove the nesting of the web.dev/stest/atest/prod.config files either by removing the element in csproj, a nesting add-on for VS or the File Nesting context menu item in VS 2017

    A note from the documentation stated XML transformation notes that:

    By default, MSBuild applies the transformation as it generates the web package if the element is already present in the transform file in the *.csproj file. In such cases, the Azure App Service Deploy task will fail because there is no further transformation applied on the Web.config file. Therefore, it is recommended that the element is removed from all the transform files to disable any build-time configuration when using XML transformation.

  • Make sure that the *.config files have the 'Copy to Output Directory' property set to if newer or always

  • Build the solution locally and check the contents of bin\release folder and make sure the web.*.config files are included

  • Run a build in VSTS and make sure the web.*.config files are included in the zipped package

  • Check the checkbox XML transformation on the Azure App Service Deploy task: enter image description here

There is a great document about how to Using XML Transformations when deploying to Azure App Service using VSTS, you can check it for some more details.

BTW, since you just need to change the port value, you can use the option XML variable substitution on the the Azure App Service Deploy task.

Ticket for details: How to transform Web.Config file 'Properly' with VSTS!

Hope this helps.