首页 > 解决方案 > System BadImageFormatException 可执行文件 (.exe) 或库 (.dll) 的格式无效

问题描述

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

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

  1. 构建作业:构建二进制文件并将它们发布到名称为 pre-drop 的工件。

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

  3. 发布作业:发布要删除的工件(用于发布管道)。

不确定我是否能够将我的想法转化为 .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 作为工件的一部分。

标签: .net-coreazure-devopsxunit.netvstestmicrosoft-extensions-logging

解决方案


BadImageFormatException Class的文档说在以下情况下会引发此异常:

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

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

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

如果以上更改平台不起作用。您也可以尝试此解决方法来添加 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:
      ...

您也可以查看此线程


推荐阅读