【问题标题】:System BadImageFormatException Format of the executable (.exe) or library (.dll) is invalidSystem BadImageFormatException 可执行文件 (.exe) 或库 (.dll) 的格式无效
【发布时间】:2020-08-05 01:14:15
【问题描述】:

CI 流水线大约需要 50 分钟才能完成,并且大部分时间都用于测试。有大量的单元测试和数据驱动测试。已决定并行运行测试,并且该方法基于此文档 Run Tests In Parallel In Build Pipelines

想法是将管道分成 3 个作业

  1. Build Job:构建二进制文件并将它们发布到工件 名称预删除。

  2. 测试作业:下载工件预放置,提取文件,使用 VSTest@2 任务并行运行测试

  3. 发布作业:将工件发布到 drop(用于发布管道)。

不确定我是否能够将我的想法转化为 .yml。

测试作业

- job : 'TestJob'
  pool:
    vmImage: windows-latest
  strategy:
    parallel: 2
  dependsOn: 'BuildJob'

  steps:

  - task: DownloadBuildArtifacts@0
    inputs:
      buildType: 'current'
      downloadType: 'single'
      artifactName: 'predrop'
      downloadPath: '$(System.ArtifactsDirectory)'

  - task: ExtractFiles@1
    inputs:
      archiveFilePatterns: '$(System.ArtifactsDirectory)/predrop/predrop.zip'
      destinationFolder: '$(System.ArtifactsDirectory)/predrop/Extpredrop'

  - task: VSTest@2
    inputs:
      testSelector: 'testAssemblies'
      testAssemblyVer2: |
       **\*tests.dll
       !**\*TestAdapter.dll
       !**\obj\**
      searchFolder: '$(System.ArtifactsDirectory)'
      vstestLocationMethod: 'location'
      vstestLocation: 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\Extensions\TestPlatform\'
      otherConsoleOptions: '/platform:x64 /Framework:.NETCoreApp,Version=v3.1'

问题在于 VSTest 任务识别并运行一些测试,但在其他测试中出错,并在某些测试中出现以下错误

System.BadImageFormatException : Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=3.1.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. 
Format of the executable (.exe) or library (.dll) is invalid.

第一个作业的二进制文件已生成 Microsoft.Extensions.Logging.Abstractions.dll 作为工件的一部分。

【问题讨论】:

  • 一切看起来都很好,所以它可能是一个单一的配置。如果您与我们分享您的 YAML 文件会很方便。
  • 为测试作业更新了 yaml

标签: .net-core azure-devops xunit.net vstest microsoft-extensions-logging


【解决方案1】:

BadImageFormatException Class 的文档说这个异常是在以下场景中抛出的:

  • DLL 或可执行文件作为 64 位程序集加载,但它包含 32 位功能或资源。例如,它依赖于 COM 互操作或调用 32 位动态链接库中的方法。

  • 要解决此异常,请将项目的平台目标属性设置为 x86(而不是 x64 或 AnyCPU)并重新编译。

因此,您可以尝试配置 VSBuild 任务以将项目重建为 x86 或 x64。查看this thread 中的类似错误。

如果以上更改平台不起作用。您也可以尝试此解决方法来添加 VSBuild 任务以在作业 TestJob 中构建您的项目。这样,就不需要下载提取作业TestJob中的工件了。下面的例子:

- job : 'TestJob'
  pool:
    vmImage: windows-latest
  strategy:
    parallel: 2
  dependsOn: 'BuildJob'

  steps:
  - task: VSBuild@1
    inputs:
      solution: '**/*.sln'
      platform: "any cpu"
      configuration: 'Release'

  - task: VSTest@2
    inputs:
      ...

您也可以查看this thread

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 2012-03-13
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多