0
votes

I have two environments, Dev and Prod, I am using XML variable Substitution to change the values according to the environment.

What I am trying to do is change the value of a specific attribute. For instance, I have the following:

<client>
 <endpoint address="url_1" binding="binding_1" bindingConfiguration="configuration_1" contract="contract_1" name="firstLink" />
 <endpoint address="url_2" binding="binding_2" bindingConfiguration="configuration_2" contract="contract_2" name="secondLink" />
</client>

and I want to substitute the address values to url_3 and the second endpoint to url_4. So, it would look something like this:

<client>
 <endpoint address="url_3" binding="binding_3" bindingConfiguration="configuration_3" contract="contract_3" name="thirdLink" />
 <endpoint address="url_4" binding="binding_4" bindingConfiguration="configuration_4" contract="contract_4" name="fourthLink" />
</client>

How it is on the DevOps side is that there is a key, value, environment pairs where the key is the name.

is there a way to specifically change the address using the same convention? Address is what I am trying to change, and I am not allowed install any outside extensions.

2

2 Answers

2
votes

As you see in docs:

Variable substitution takes effect only on the applicationSettings, appSettings, connectionStrings, and configSections elements of configuration files. If you are looking to substitute values outside of these elements you can use a (parameters.xml) file, however you will need to use a 3rd party pipeline task to handle the variable substitution.

So in your case you can use token replace

You just need to put tokens in your file like this:

<client>
 <endpoint address="#{URL_1}#" binding="binding_1" bindingConfiguration="configuration_1" contract="contract_1" name="firstLink" />
 <endpoint address="#{URL_1}#" binding="binding_2" bindingConfiguration="configuration_2" contract="contract_2" name="secondLink" />
</client>

Where URL_1 and URL_2 are variables defined in the pipeline.

0
votes

As stated in the documentation, variable substitution only works on some specific elements of the configuration files. If the configuration file you want to substitute does not belong to applicationSettings, appSettings, connectionStrings, and configSections elements and you are not allowed to use any outside extensions, then I think you need to use File Transform.

You can transform the values in the config file by creating a Transform file. For related syntax, please follow Web.config Transformation Syntax.

We can also use the built-in File Transform task to transform.

To apply XML transformation to configuration files (*.config) you must specify a newline-separated list of transformation file rules using the syntax:

-transform <path to the transform file> -xml <path to the source file> -result <path to the result file>

For example:

enter image description here

For details, you can refer to this case.

XML transformation notes:

By default, MSBuild applies the transformation as it generates the web package if the <DependentUpon> 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 <DependentUpon> element is removed from all the transform files to disable any build-time configuration when using XML transformation.