45

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?

poke
  • 369,085
  • 72
  • 557
  • 602
Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • It seems fixed in 2.2 SDK https://github.com/dotnet/sdk/pull/2533 Test projects can have a versionless asp.net core reference that will match the referenced project – Michael Freidgeim Feb 08 '19 at 23:57

3 Answers3

74

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="..\AspNetCoreAppToTest\AspNetCoreAppToTest.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="..\AspNetCoreAppToTest\AspNetCoreAppToTest.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.

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • 1
    I was following your advice regarding the first alternative, and got the following compiler error on build. What am I doing wrong? Error CS5001 Program does not contain a static 'Main' method suitable for an entry point – Hermann.Gruber Oct 31 '18 at 09:32
  • 1
    That seems like a different problem because the test sdk turns the project into an executable but also emits a main method which somehow doesn't work with your project – Martin Ullrich Oct 31 '18 at 09:49
  • Thanks for the clarification! Well, but this seems to happen also if you start over from scratch: I've made a new asp.net core MVC web app, chose 2.1 and MVC. Then I added a new .NET core class library, replaced the project file with the above snippet and got the compile error. The result is here: https://github.com/HermannGruber/AspNetCoreAppToTest – Hermann.Gruber Oct 31 '18 at 11:42
  • 1
    Update: I've noticed that the compiler error disappears if we change the project output type to "class library". Or equivalently, if we add Library to the first property group in the code snippet in your answer. – Hermann.Gruber Oct 31 '18 at 11:45
  • Using VS 2019, I had to also close and re-open VS + rebuild to remove the errors. – juunas Jan 07 '19 at 07:12
  • This is the only solution that will solve the issue for test projects that reference a .net core 2.1 app. THANK YOU – PetMarion Feb 21 '19 at 20:26
  • For ASP.NET Core 2.2 alternative 2 is enough, but got a warning about specifying a version. It works and does not give a warning without a version, i.e. just `` – EM0 May 09 '19 at 10:02
12

The question specifies ASP.NET Core 2.1, but for those running into the same problem with ASP.NET Core 2.2 it's enough to add the following to your test project file:

<PackageReference Include="Microsoft.AspNetCore.App" />

There is no need to set Sdk="Microsoft.NET.Sdk.Web" anymore and you should not add a version to the package reference above - that causes a warning.

EM0
  • 5,369
  • 7
  • 51
  • 85
-2

I was having the same problem. Add this to Test Project solved the problem

 <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.1"/>
debugguru
  • 60
  • 6