.net-core - System BadImageFormatException 可执行文件 (.exe) 或库 (.dll) 的格式无效

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

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

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

  1. 构建作业:构建二进制文件并将其发布到工件 预删除名称。

  2. 测试作业:下载预置工件、提取文件、使用 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 作为工件的一部分。

最佳答案

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

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

  • 要解决此异常,请将项目的 Platform target 属性设置为 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 .

关于.net-core - System BadImageFormatException 可执行文件 (.exe) 或库 (.dll) 的格式无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61357970/

相关文章:

.net - 使用 .NET 核心 2.2 中的 MailKit SMTP 客户端使用 Gmail SMTP 发送邮件对我不起作用

c# - 为什么 System.Text Json Serializer 不会序列化这个通用属性,但 Json.NET 会?

azure-devops - 在 Visual Studio Team Services 上设置 TrustedHosts

cmd - tf.exe 提示 "TF30063: You are not authorized to access "

azure-devops - VS402864 : No artifact type found corresponding to id PipelineArtifact

c# - 如何从 .Net 核心应用程序中的 SQL 语句获取标量值?

.net-core - 如何在 AWS Cloudwatch 上的 Serilog 的结构化日志输出中显示带有替换参数的日志消息

c# - 我的 XUnit dotnet 核心项目在我的桌面上构建,但不在 DevOps Server 2019 中构建

c# - 在单个测试类中测试接口(interface)的多个实现

c# - Jenkins 是否支持 XUnit.Net?