Why I can't consolidate projects sdk versions

2019-06-23 15:32发布

问题:

First of all, I'm getting this build error, but only on some machines:

Error CS1705 Assembly '***' with identity '***, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'System.Net.Http, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'System.Net.Http' with identity 'System.Net.Http, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' *** C:\***\CSC 1 Active

I guess, it's somehow connected with SDK versions. From what I can see in Properties window, on some projects I have Microsoft.NETCore.App 1.1.1 while others have Microsoft.NETCore.App 1.1.2.

After package update, build succeeds (but why I am forced to update all packages?).

But still, sdk versions differ. And now I'm trying to use this consolidation feature of Visual Studio:

which says in a tooltip:

Following versions are unavaible due to additional constraints in the project or packages.config

How to upgrade all projects to newer SDK? And how to be sure not to break build on other machines in future?

回答1:

If you manually upgraded before, a Microsoft.NETCore.App package on some projects, they will contain an element like <PackageReference Update="Microsoft.NETCore.App" Version="1.1.1" />.

The reason is that the Microsoft.NET.Sdk SDK creates an implicit package reference.

Since the package is implicitly referenced, NuGet should not have done this in the first place and the current VS updates no longer allow to update implicitly referenced packages.

You can do two things here:

  1. Remove all PackageReference elements that change/set the version of Microsoft.NET.Sdk. This will then let the SDK version (included in MSBuild / dotnet cli) choose the version.
  2. 1 + In a <PropertyGroup> of your csproj files, set

     <RuntimeFrameworkVersion>1.1.2</RuntimeFrameworkVersion>
    

    This will set the version that the implicit reference of the SDK will use.

  3. 1 + In a <PropertyGroup> of your csproj files, set

    <DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
    

    And then install the desired version of Microsoft.NETCore.App manually

I recommend going with Option 1 since it doesn't require you to modify csproj files any more (e.g. when adding new projects, restructuring solutions etc.).



回答2:

Had the same problem in a ASP.NET Core 2.0 project - this worked for me:

Edit your 'myproject.csproj' file and add/update with the following:

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
    <TargetLatestRuntimePatch>true</TargetLatestRuntimePatch> // add this line.
  </PropertyGroup>