Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
639 views
in Technique[技术] by (71.8m points)

testing - Integration and unit tests no longer work on ASP.NET Core 2.1 failing to find assemblies at runtime

When creating test projects or upgrading an application and tests to ASP.NET Core 2.1 / .NET Core 2.1, running tests fails with assembly load exceptions like

System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

When adding references to some other libraries there are also build warnings like

warning MSB3277: Found conflicts between different versions of "Microsoft.Extensions.Options" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.Extensions.Configuration.Abstractions" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.AspNetCore.Hosting.Abstractions" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.Extensions.DependencyInjection.Abstractions" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.AspNetCore.Http.Abstractions" that could not be resolved.
warning MSB3277: Found conflicts between different versions of "Microsoft.AspNetCore.Http.Features" that could not be resolved.

How can I make test projects work for testing ASP.NET Core 2.1 applications?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update: This has been made easier with 2.2 Tooling. Make sure that your dotnet --version SDK version is at least 2.2.100, even when buidling 2.1 applications

Just add a versionless package reference to your project while keeping the Microsoft.NET.Sdk:

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

      <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
      </PropertyGroup>

      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.1" />
        <PackageReference Include="Microsoft.AspNetCore.App" />
        <!-- other references to xunit, test SDK etc. -->
      </ItemGroup>

      <ItemGroup>
        <ProjectReference Include="..AspNetCoreAppToTestAspNetCoreAppToTest.csproj" />
      </ItemGroup>

    </Project>

Original:

ASP.NET Core 2.1 uses a new "shared framework" to run ASP.NET Core applications on. Test projects need to be modified/updated to also use this shared framework using one of the following approaches:

  1. Change the test project's <Project> tag in the first line to use the web SDK (Microsoft.NET.Sdk.Web instead of Microsoft.NET.Sdk) and add a package reference to Microsoft.AspNetCore.App (or .All if you are using that inside the web project) without specifying a version

    The project file (.csproj) of the test project should now look like this:

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
      <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.1" />
        <PackageReference Include="Microsoft.AspNetCore.App" />
        <!-- other references to xunit, test SDK etc. -->
      </ItemGroup>
    
      <ItemGroup>
        <ProjectReference Include="..AspNetCoreAppToTestAspNetCoreAppToTest.csproj" />
      </ItemGroup>
    
    </Project>
    
  2. Alternative: Leave the Sdk as-is and add a PackageReference to the shared framework package but specify a version.

    This can be done by simply adding a NuGet reference to Microsoft.AspNetCore.App. However, this may cause issues since the SDK may choose to update the reference when a new patch release of the ASP.NET Core is released and the tooling is updated to reflect this. You will need update the NuGet reference for every patch release.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...