我正在使用 FileTransform@2 任务在 Azure DevOps Pipeline (yaml) 中使用 web.[environment].config 转换 web.config。它似乎在其中一个单独的转换上失败,从而导致整个工作失败,尽管我不确定为什么。
这是任务的错误消息:
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
所以看起来它不喜欢 PCWSUser appSetting。
这是 PCWSUser 的 web.config 片段:
...
<add key="PCWSUser" value="TheUserName" />
...
这是 PCWSUser 的 web.[environment].config(在本例中为 web.qa.config)片段:
...
<add key="PCWSUser" value="TheUserNameQA" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
...
我不太确定我做错了什么...当转换在 Visual Studio 中本地完成时,它没有任何问题。另一个奇怪的事情是我已经运行了几次,似乎每次都会选择不同的应用程序设置来出错。相同的错误消息等等,只是设置不同。所有设置均按此方式设置,仅供参考。
如果您需要更多信息,请告诉我。
编辑1
根据@Kevin Lu-MSFT的建议,我将
/p:TransformWebConfigEnabled=false
添加到构建步骤中并再次尝试。
构建阶段日志:
##[debug]INPUT_MSBUILDARGS: '/t:rebuild /p:DeployOnBuild=true /p:PublishProfile="Dev" /p:PackageLocation="D:\agent\_work\283\a" /p:TransformWebConfigEnabled=false'
但是,尽管错误再次移动,但转换仍然失败。这次错误发生在两个步骤之间,所以我什至不清楚出了什么问题。
部署阶段日志:
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
根据我的测试,
FileTransform task
可以成功转换web.config文件。
步骤如下(直接转换文件,无需构建工程),大家可以参考一下。
Step1:文件结构。您需要确保这些文件位于同一文件夹中。
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>
第 2 步:将
FileTransform task
与 Yaml 一起使用。
- task: FileTransform@2
inputs:
folderPath: 'configfolder'
xmlTransformationRules: '-transform **\web.qa.config -xml **\web.config'
然后任务就可以成功运行了。但如果文件夹中包含多个转换文件,则可能会导致错误。
另一方面, 如果
FileTransform task
在构建步骤之后,您需要确保构建任务不会转换 web.config 文件。
您可以在构建任务中添加 Msbuild 参数
/p:TransformWebConfigEnabled=false
。
这里是关于这个问题的讨论。