在 NUnit 测试中加载 ASP.NET Core 参考程序集时出现 System.BadImageFormatException

问题描述 投票:0回答:0

我有一个 ASP.NET Core 应用程序项目和集成测试项目。在每个集成测试应用程序中都托管有

WebApplicationFactory<TEntry>
,ASP.NET Core 项目和
Microsoft.AspNetCore.Mvc.Testing
包由测试项目引用。

TargetFramework: net60
ASP.NET Core Framework: 6.0.0
Nunit: 3.13.3
NUnit3TestAdapter: 4.4.0
Visual Studio: 17.2.0

当我尝试运行以下异常的任何测试时:

System.BadImageFormatException:无法加载文件或程序集“System.Transactions.Local,Version=6.0.0.0,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51”。不应为执行加载引用程序集。它们只能在仅反射加载器上下文中加载。 (0x80131058)

System.BadImageFormatException:无法加载文件或程序集“System.Transactions.Local,Version=6.0.0.0,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51,processorArchitecture=MSIL”。不应为执行加载引用程序集。它们只能在仅反射加载器上下文中加载。 (0x80131058)

System.BadImageFormatException:无法加载参考程序集以执行。

堆栈跟踪:
MyIntegrationTests.MyIntegrationTest()
--BadImageFormatException
AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext,字符串 ilPath,字符串 niPath,ObjectHandleOnStack retAssembly)
AssemblyLoadContext.LoadFromAssemblyPath(字符串 assemblyPath)
TestAssemblyResolver.OnResolving(AssemblyLoadContext 上下文,AssemblyName 名称) TestAssemblyResolver.Resolve(AssemblyLoadContext 上下文,AssemblyName 名称) TestAssemblyLoadContext.Load(AssemblyName 名称) AssemblyLoadContext.ResolveUsingLoad(AssemblyName 程序集名称) AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext,AssemblyName assemblyName) --BadImageFormatException

经过长时间的调查,我发现在测试项目的输出目录中创建了带有“reference”框架程序集的“refs”文件夹,而 NUnit 引擎无法加载任何这些参考程序集:

 Directory of D:\Repo\MyProject\MyIntegrationTests\bin\Debug\net60\refs

03/22/2023  04:17 PM    <DIR>          .
03/22/2023  04:17 PM    <DIR>          ..
10/26/2021  09:52 PM            29,824 Microsoft.AspNetCore.Antiforgery.dll
10/26/2021  09:51 PM            25,728 Microsoft.AspNetCore.Authentication.Abstractions.dll
10/26/2021  09:52 PM            28,288 Microsoft.AspNetCore.Authentication.Cookies.dll
10/26/2021  09:52 PM            22,656 Microsoft.AspNetCore.Authentication.Core.dll
10/26/2021  09:52 PM            38,528 Microsoft.AspNetCore.Authentication.dll
....

有没有办法禁用“refs”文件夹生成或强制 NUnit 引擎加载这些引用程序集?

我找到的唯一可行的解决方案是在使用 msbuild 任务构建后手动删除“refs”文件夹。如果需要,我可以尝试创建最小的可重现示例。

这是测试项目的

.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net60</TargetFramework>
    <AssemblyName>MyIntegrationTests</AssemblyName>
    <RootNamespace>MyIntegrationTests</RootNamespace>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    <!-- This doesnt help to avoid load assembly exception -->
    <ProduceReferenceAssembly>false</ProduceReferenceAssembly>
    <ProduceOnlyReferenceAssembly>false</ProduceOnlyReferenceAssembly>
    <PreserveCompilationContext>false</PreserveCompilationContext>
    <OutputType>Library</OutputType>
  </PropertyGroup>

  <!-- This doesnt help to avoid load assembly exception -->
  <ItemGroup>
    <KnownFrameworkReference Update="Microsoft.AspNetCore.App">
      <TargetingPackVersion>6.0.0</TargetingPackVersion>
    </KnownFrameworkReference>
  </ItemGroup>

  <ItemGroup>
    <Content Include="appsettings.json">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="FluentAssertions" Version="5.3.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.13" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
    <PackageReference Include="NUnit" Version="3.13.3" />
    <PackageReference Include="NUnit3TestAdapter" Version="4.4.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\MyApplication.csproj" />
  </ItemGroup>
  
  <!-- This helps to avoid load assembly exception -->
  <Target Name="RemoveRefs" AfterTargets="Build">
    <Warning Text="Remove folder with reference assemblies to support Nunit Runner : $(ProjectDir)$(OutputPath)refs"></Warning>
    <RemoveDir Directories="$(ProjectDir)$(OutputPath)refs"></RemoveDir>
  </Target>
</Project>
c# asp.net-core nunit nunit-3.0
© www.soinside.com 2019 - 2024. All rights reserved.