简体   繁体   中英

FileTransform task in Azure DevOps throws error on transform

I am using the FileTransform@2 task to transform a web.config with a web.[environment].config in a Azure DevOps Pipeline (yaml). It seems to fail on one of the individual transforms which fails the whole job, though I'm not sure why.

Here is the error message from the task:

Executing SetAttributes (transform line 72, 48)
on /configuration/appSettings/add[@key='PCWSUser']
System.NullReferenceException: Object reference not set to an instance of an object.
Applying to 'add' element (no source line info)
   at Microsoft.Web.XmlTransform.XmlTransformationLogger.ConvertUriToFileName(XmlDocument xmlDocument)
Set 'key' attribute
   at Microsoft.Web.XmlTransform.XmlTransformationLogger.LogWarning(XmlNode referenceNode, String message, Object[] messageArgs)
Set 'value' attribute
   at Microsoft.Web.XmlTransform.Transform.ApplyOnAllTargetNodes()
Set 2 attributes
Done executing SetAttributes

So it looks like it doesn't like the PCWSUser appSetting.

Here's the web.config snippet for PCWSUser:

...
<add key="PCWSUser" value="TheUserName" />
...

Here's the web.[environment].config (in this case web.qa.config) snippet for PCWSUser:

...
<add key="PCWSUser" value="TheUserNameQA" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
...

I'm not really sure what I'm doing wrong... When the transform is done locally in Visual Studio it doesn't have any problem with it. Another strange thing is I've ran this a few times and it seems to pick a different appSetting to error out on each time. Same error message and all, just different settings. All the settings are set up this way FYI.

Let me know if you need any more info.

EDIT 1

As per @Kevin Lu-MSFT suggestion, I added /p:TransformWebConfigEnabled=false to the build step and tried again.

Build stage logs:

##[debug]INPUT_MSBUILDARGS: '/t:rebuild /p:DeployOnBuild=true /p:PublishProfile="Dev" /p:PackageLocation="D:\agent\_work\283\a" /p:TransformWebConfigEnabled=false'

However, the transform still failed, although the error moved around again. This time the error is in between 2 steps so Im not even clear what went wrong.

Deploy stage logs:

Executing Replace (transform line 10, 105)
on /configuration/connectionStrings/add[@name='SqlConnectionString']
Applying to 'add' element (no source line info)
Replaced 'add' element
Done executing Replace
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Web.XmlTransform.XmlTransformationLogger.ConvertUriToFileName(XmlDocument xmlDocument)
   at Microsoft.Web.XmlTransform.XmlTransformationLogger.LogWarning(XmlNode referenceNode, String message, Object[] messageArgs)
   at Microsoft.Web.XmlTransform.Transform.ApplyOnAllTargetNodes()
Executing Replace (transform line 11, 105)
on /configuration/connectionStrings/add[@name='DB2ConnectionString']
Applying to 'add' element (no source line info)
Replaced 'add' element
Done executing Replace

Based on my test, the FileTransform task could transform the web.config file successfully.

Here are the steps (directly transform the file without build the project), you could refer to them.

Step1: File structure. You need to make sure that the files are in the same folder.

文件

Web.config

<configuration>
  <connectionStrings>

  <appSettings>
....
    <add key="PCWSUser" value="TheUserName" />
  </appSettings>

</configuration>

web.qa.config

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
   ....
  <appSettings>
    <add key="PCWSUser" value="TheUserNameQA" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
  </appSettings>

</configuration>

Step2: Use the FileTransform task with Yaml.

  - task: FileTransform@2
      inputs:
        folderPath: 'configfolder'
        xmlTransformationRules: '-transform **\web.qa.config -xml **\web.config'  

Then the task could run successfully. But if the folder contains multiple transform files, it may cause error.

On the other hand, if the FileTransform task is after the build step,you need to make sure the build task doesn't transform the web.config file.

You could add the Msbuild arguments /p:TransformWebConfigEnabled=false in the build task.

在此处输入图像描述

Here is a discussion about this issue.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM