sdk: Implicit references fail transitively
This issue is effectively the same issue @natemcmaster has reported in https://github.com/aspnet/Docs/issues/9490#issuecomment-442912739 for .NET Core 2.2 -> 3.0 migrations, but the regression goes back to at least 2.1 -> 2.2 upgrades as well:
Preview 1 Known Issue - workaround for NuGet/Home#7342 - projects which do not start with
<Project Sdk="Microsoft.NET.Sdk.Web">will get compiler or runtime errors due to missing Microsoft.AspNetCore.* assemblies. This is most often the case for test projects and class libraries.
Consider the following project structure:
Source
|
\ WebApi
|
\ WebApi.csproj
Tests
|
\ WebApi.Tests
|
\ WebApi.Tests.csproj
and the following definitions for WebApi.csproj and WebApi.Tests.csproj:
WebApi.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<!-- Implicit Version to avoid MSBuild targets warning -->
<PackageReference Include="Microsoft.AspNetCore.All" />
<!--<PackageReference Include="Microsoft.AspNetCore.All" Version="2.2.3" AllowExplicitVersion="true" />-->
</Project>
WebApi.Tests.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<!-- This could really be any ProjectReference that transitively references a metapackage -->
<ProjectReference Include="..\WebApi\WebApi.csproj" />
</ItemGroup>
</Project>
If you build the solution, Tests\WebApi.Tests\obj\project.assets.json will show references to Microsoft.AspNetCore.All metapackage for .NET Core 2.1
The only solution is to use AllowExplicitVersion="true":
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.2.3" AllowExplicitVersion="true" />
The error message is:
Severity: Error
Code: CS1705
Description: Assembly 'WebApi' with identity 'WebApi, Version=1.5.0.0, Culture=neutral, PublicKeyToken=null' uses 'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' which has a higher version than referenced assembly 'Microsoft.AspNetCore.Mvc.ViewFeatures' with identity 'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
Project: WebApi.Tests
File: D:\source\John.Zabroski\Source\WebApi.Tests\CSC
Line: 1
Suppression State: Active
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 17 (5 by maintainers)
Commits related to this issue
- Update dependencies from https://github.com/aspnet/AspNetCore-Tooling build 20191002.2 (#3046) - Microsoft.NET.Sdk.Razor - 3.1.0-preview1.19502.2 — committed to dsplaisted/sdk by dotnet-maestro[bot] 5 years ago
The transitivity issue isn’t related to whether your project is clean. It’s just how the reference to ASP.NET works in .NET Core 2.2.
We’ve been iterating on how the ASP.NET dependency is expressed since .NET Core 1.0. While not ideal, the behavior in 2.2 is improved over 2.1, 2.0, etc. We’ve made some more fundamental changes for .NET Core 3.0, which we believe will be a big improvement and resolve the issues we’ve had in previous versions with how the ASP.NET dependency works.
@jzabroski This scenario will be fixed in .NET Core 3.0, which will use FrameworkReferences which will flow transitively.
For 2.2, the ASP.NET Core PackageReference does not flow transitively. However, you should be able to add it to the test project (without a version number, the same as in the web project), and it should work correctly. Have you tried that?