1
votes

I've been using Replace Tokens (from VS Marketplace) and it served me well so far for the dynamic connection string substitution in the web.config file. The way it works, in the Web.Release.config I have a construction like this:

<connectionStrings>
    <add name="Default"
        connectionString="__AzureDBConnectionString__"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

When I start release in VSTS, it replaces this token (in between "__") with the variable value in the release pipeline. So, I always have a connection string that corresponds to the relevant environment. Yesterday I decided to add some more variables into this file, hoping it'll work as well, as I have a lot keys that should be parametrized for security reasons. Well, if I add to the file something like this:

<appSettings>
        <add key="mailToken" value="__mailToken__" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>

it doesn't work. In the web.config which is deployed, I see value="mailToken" i.e. it's not substituted with the variable stored in the release pipeline (as for connection string). After around a day of suffering, I discovered that the only parameter that could be replaced like that is connection string (because this is the only parameter set in auto-generated by the build "Web.SetParameters.xml" file). I tried to play around parameter.xml file as some sources suggest, but no success. It is important that all the variables are taken from the release pipeline variable list (and not hardcoded in config files). Can someone suggest a proper way to substitute the values in appSetting or another workaround (without Replace Tokens task etc.)?

2
Do you solve this issue?starian chen-MSFT

2 Answers

2
votes

According to your description, you are using Web Deployment Package (Web.SetParameters.xml), by default the appSettings in Web.Release.config won’t be added to Web.SetParameters.xml file, but will replace the value in web.config file directly when generate the Web Deployment package, so the value of mailToken in web.config file (in zip package file) will be mailToken.

So, the simple way to replace it is using XML variable substitution in File Transforms & Variable Substitution Options section of IIS Web App Deploy or Azure App Service Deploy task. You just need to add the variable with the same name of key value (mailToken) to the release definition and check the XML variable substitution option. (Do not need to configure it in Web.Release.config)

More information, you can refer to: XML variable substitution

0
votes

I struggled with something similar recently too, only today did I come to a solution. My main hangup was defining the parameters.xml file in my project directory, as this conflicted with some of the auto replacement logic defined in the vsts-tasks release task for web deploy. I'd only define parameters.xml for properties that won't be handled automatically through VSTS.

Also, I'd suggest adding this msbuild parameter to your build job under "Build Solution", this will prevent VSTS from adding connection strings in the setparameters.xml file of your web deploy package.

/p:AutoParameterizationWebConfigConnectionStrings=false

Ultimately if this doesn't work, you'll want to turn on System.debug to true in your environmental variables so you can see exactly what is going on during release. Unfortunately the debug logging you'll see with system.debug on aren't that great, but they may help.

Good luck!