'How to do file transforms / variable substitution with Visual Studio Online and .NET Desktop App
Visual Studio Team Services Online (www.visualstudio.com) Release Definitions has a setting within IIS Web App Deploy
to perform XML variable substitution. Based on the in-line help:
Variables defined in the Build or Release Definition will be matched against the 'key' or 'name' entries in the appSettings, applicationSettings, and connectionStrings sections of any config file and parameters.xml. Variable Substitution is run after config transforms.
Is there any such simple Task that can be applied when deploying .NET Desktop-oriented releases (for desktop apps or Windows services)? Specifically, I want to replace the value
part of key-value elements of appSettings.config
files (or, post-build they are now *.exe.config
).
I've seen some token replacement marketplace add-ons, but those don't do the trick. They'll replace the value
part of appSetting, but not based on key
. I could probably shoe-horn it, but its inelegant & fragile.
Solution 1:[1]
Based on @Rodrigo-werlang's answer, here's a more complete solution:
1) Create the XDT transformation file. I followed one thread that had me editing the .csproj file to create app.debug.config
, etc. but ultimately this isn't needed. I created my file as Release.config
, and used a web.release.config
file as the basis of the transformations.
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="MagicKey" value="#{MagicKey}#" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
2) In the Release definition, add a Task for XDT Transformation. Apply the Release.config
settings to the right .exe.config
file. NOTE THE WORKING FOLDER - It defaults to the source code directory, and I wanted to apply to the build artifact folder.
3) Add a Task for Tokenization. I used Guillaume Rouchon's Replace Tokens, though I suspect any tokenizer will work fine. Again note the working directory.
4) Set your release variables per normal.
Solution 2:[2]
You can add this two vsts extensions to do the job.
https://marketplace.visualstudio.com/items?itemName=qetza.xdttransform
https://marketplace.visualstudio.com/items?itemName=ms-devlabs.utilitytasks From this second you want to use tokenizer task.
Both of them are good documented so that you can implement it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jklemmack |
Solution 2 | Rodrigo Werlang |