【问题标题】:FileTransform task in Azure DevOps throws error on transformAzure DevOps 中的 FileTransform 任务在转换时引发错误
【发布时间】:2020-07-09 22:25:40
【问题描述】:

我正在使用 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 sn-p:

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

这是 PCWSUser 的 web.[environment].config(在本例中为 web.qa.config)sn-p:

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

我不太确定自己做错了什么......当转换在 Visual Studio 中本地完成时,它没有任何问题。另一个奇怪的事情是我已经运行了几次,它似乎每次都选择了不同的 appSetting 来出错。相同的错误消息和所有,只是不同的设置。所有设置都以这种方式设置仅供参考。

如果您需要更多信息,请告诉我。

编辑 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

【问题讨论】:

  • 你有这些值作为管道变量吗?
  • 您可以参考答案中的故障排除步骤。您可以尝试直接在管道中运行任务以转换文件。如果没有帮助,您可以与我们分享构建定义。
  • @Crowcoder 它是一个混合体。我有实际的单独文件(在这种情况下为 web.config 和 web.qa.config),但连接字符串通过替换来自 Keyvault。替代似乎很好。 Keyvault 任务将它们作为变量注入到管道中。

标签: c# asp.net web-config azure-pipelines web.config-transform


【解决方案1】:

根据我的测试,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

这里是a discussion 关于这个问题。

希望这会有所帮助。

【讨论】:

  • Kevin,你说得对,在构建阶段也发生了转换,所以我添加了你的 /p:TransformWebConfigEnabled=false 标志并再次尝试。它仍然失败,但这次出现了不同的错误消息。如果您不介意,请在Edit 1 下了解有关原始问题的更多详细信息。
  • 此外,该文件夹确实包含多个配置转换文件(web.dev.config、web.qa.config、web.shadow.config 和 web.prod.config),但我要告诉它xmlTransformationRules 设置中的特定设置。
  • 要缩小此问题的范围,您可以直接使用文件转换任务来转换 web.config 文件(无需构建步骤)。请检查它是否可以工作。如果它可以工作,这个问题仍然与构建步骤有关。您可以参考this ticket
  • Kevin,我确实注意到部分问题是它转换了错误的 web.config(它会抛出一个错误,因为没有连接字符串节点,所以无法插入连接字符串)这是因为我的解决方案有一些 web.configs。我怎样才能确保它只转换主 web.config?这是我的转换规则:'-transform **\Web.$(environment).config -xml **\Web.config'
  • 您可以尝试将target file 参数添加到任务:targetFiles: web.config。请检查它是否可以确保正确的文件。
猜你喜欢
  • 2020-09-01
  • 2021-07-19
  • 1970-01-01
  • 2021-07-26
  • 2022-08-24
  • 1970-01-01
  • 2020-07-19
  • 2019-10-25
  • 1970-01-01
相关资源
最近更新 更多