5
votes

I've got a .Net Framework 4.6.1 project, which creates a Windows Service. We want to have this built and deployed by an Azure Devops pipeline. We've successfully created pipelines for all of our .Net Core web services (using the ASPNETCORE_ENVIRONMENT variable and a set of appsettings.{ENV}.json files) but the Windows Services don't seem to play as nicely in terms of providing different configuration for different environments.

I've got XDT transformations working for the App.config XML file. I can right click the files in Visual Studio and "Preview Transform" and it works fine.

What's not clear is how I translate this to the pipeline configuration. The Build task seems to be capable of running the transform but then I'm not sure how we have a different transformation for different Deploy stages. What happens instead is that all environments end up with whatever transformation was applied to the Build Configuration selected as part of the Build step.

I've followed numerous blog posts - including this one, which claims you can have multiple build configurations, but for which I don't seem to have the setting.

What am I missing?

2

2 Answers

3
votes

What we do with VSTS which I assume is similar in the newer Azure Devops. We have the transformation add tokens to the config file IE "DefaultConnection": "Server=DBServer;Database=DB;User ID=DBUser;Password=DBPassword;" Then There is a task on the release enviroment called Tokenization which pulls variables set from the release enviorment to replace the underscored tokens.

We use this plugin to do the tokenization step. https://marketplace.visualstudio.com/items?itemName=TotalALM.totalalm-tokenization

2
votes

Further to Ryan Schlueter's answer, instead of using the third-party Tokenization task, there is an official File Transform task available which does the job perfectly. The task is not available as an extension; it must be built from source and uploaded to your Azure DevOps server manually. I had a little trouble with this (now resolved), but these are the commands to run (from a VS command prompt):

git clone https://github.com/microsoft/azure-pipelines-tasks.git
cd azure-pipelines-tasks
node make.js build test --task FileTransformV1
C:\Users\jason.payne\source\repos\external\azure-pipelines-tasks\_build\Tasks\FileTransformV1
tfx build tasks upload --task-path .\_build\Tasks\FileTransformV1 -u {URL}/{Collection} -t {PAT}

where {URL}, {Collection} and {PAT} must be filled in per your own details.

You can then add this task to your release pipeline along with the desired variables as per the following help text from the task:

Variables defined in the build or release pipelines will be matched against the 'key' or 'name' entries in the appSettings, applicationSettings, and connectionStrings sections of any config file and parameters.xml.

This solution removes the need to have source-controlled transformation files containing the various environment-specific settings, and instead puts them in the release pipeline instead where they belong. You just have a base app.config with empty or default values, and no need to change its default build action properties.