0

I have a pipeline set up in Azure which uses variables to replace values in a XML config file when run. I traced a bug in the application back to the config file not having the correct value and I can see from the pipeline logs the expected value is not substituted in.

I initially thought this was because I'd used self closing tags, but replacing them with full tags didn't fix the issue, replacing them with a whitespace value didn't fix the issue.

In other respects the pipeline works fine. My testing has shown it will replace a non-empty value, with either an empty or non-empty value. But I cannot get it to replace and empty value with anything. So I don't think it's anything obvious e.g. it's not skipping substitution entirely, it can match the variables, the varaibles are in the applicationSettings section, etc. Searches only seem to show things a few years old and are generally replacing a non-empty value with an empty value, which is the opposite of what I'm trying.

I could, in the default config file put in dummy values and replace them with empty/non-empty values as required, but this would involve adding a bunch of variables to the pipeline only to substitute them with empty values in most cases. It would also mean I'd need to remember to add any new entries in the config file with a dummy value and update the pipeline accordingly.

Has anyone else come across this issue? And if so, did you resolve it and how?

An example of my config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  ...
  <applicationSettings>
    <App.Namespace>
      <setting name="Test1" serializeAs="String">
        <value />
      </setting>
      <setting name="Test2" serializeAs="String">
        <value></value>
      </setting>
      <setting name="Test3" serializeAs="String">
        <value> </value>
      </setting>
      <setting name="Test4" serializeAs="String">
        <value>dummy</value>
      </setting>
    </App.Namespace>
  </applicationSettings>
  ...
</configuration>      

Pipeline variables

Test1 -> Value1
Test2 -> Value2
Test3 -> Value3
Test4 -> Value4

Output config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  ...
  <applicationSettings>
    <App.Namespace>
      <setting name="Test1" serializeAs="String">
        <value />             <!-- Expected Value1 -->
      </setting>
      <setting name="Test2" serializeAs="String">
        <value></value>       <!-- Expected Value2 -->
      </setting>
      <setting name="Test3" serializeAs="String">
        <value> </value>      <!-- Expected Value3 -->
      </setting>
      <setting name="Test4" serializeAs="String">
        <value>Value4</value> <!-- Correct -->
      </setting>
    </App.Namespace>
  </applicationSettings>
  ...
</configuration>      
B_D
  • 225
  • 2
  • 16

1 Answers1

0

You could use Replace Tokens extension. With this extension, you could replace the token with empty or non-empty value. For example, set the value as #{var}#, if you want to use empty string, make variable var as (empty):

  <setting name="Test" serializeAs="String">
    <value>#{var}#</value>
  </setting>

If you don't want to remember aby variable/dummy name, you may try using a transform file as this case mentioned. For example:

<applicationSettings xdt:Transform="Replace">
    <App.Namespace>
      <setting name="Test1" serializeAs="String">
        <value>value1</value>
      </setting>
      <setting name="Test2" serializeAs="String">
        <value>value2</value>
      </setting>
      <setting name="Test3" serializeAs="String">
        <value>value3</value>
      </setting>
      <setting name="Test4" serializeAs="String">
        <value>value4</value>
      </setting>
    </App.Namespace>
  </applicationSettings>

But I don't think this solution is convenient than using Replace Tokens extension.

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • Thanks for the suggestions. But this has the same issues as putting in dummy values like I did for ```Test4``` above. I need to remember to put in dummy values for new values and update the pipeline to replace that value with nothing in addition to the substitutions for a non-empty value that I always want If I could get the substitution to work correctly I'd put an empty value in my config file, add only the non-empty substitutions I require and that would be it. – B_D Nov 24 '20 at 11:32
  • You don't need to remember dummy values for new values, as you don't need to change `#{var}#`, you just need to set value for variable `var`. – Cece Dong - MSFT Nov 25 '20 at 09:56
  • If you don't want to remember aby variable/dummy name, you may try using a transform file as [this case][https://stackoverflow.com/questions/11033001/how-to-change-the-value-of-attribute-in-appsettings-section-with-web-config-tran] mentioned. But I don't think it's convenient using this way. – Cece Dong - MSFT Nov 27 '20 at 09:52