如何避免 NuGet 出现兼容性警告?

Ant*_*ean 2 nuget .net-core

我正在为 Linux 构建 .NET Core 2.0 应用程序。以下是项目文件的相关部分。

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

我的软件包系统有一些不太令人敬畏的行为。我被允许添加这个包。我可能没有看到它只与 .NET Framework 4.0 兼容。

PS> dotnet add package System.Net.Http.Formatting.Extension
  Writing C:\Users\anthony.mastrean\AppData\Local\Temp\tmp4823.tmp
info : Adding PackageReference for package 'System.Net.Http.Formatting.Extension' into project 'Example.csproj'.
log  : Restoring packages for Example.csproj...
warn : Package 'System.Net.Http.Formatting.Extension 5.2.3' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
info : Package 'System.Net.Http.Formatting.Extension' is compatible with all the specified frameworks in project 'Example.csproj'.
info : PackageReference for package 'System.Net.Http.Formatting.Extension' version '5.2.3.0' added to file 'Example.csproj'.
Run Code Online (Sandbox Code Playgroud)

当我构建时,它是“成功的”,但有一个警告(为了清晰起见,被剪掉了)......

PS> dotnet build
Microsoft (R) Build Engine version 15.5.180.51428 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

...

Build succeeded.

Example.csproj : warning NU1701: Package 'System.Net.Http.Formatting.Extension 5.2.3' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
Example.csproj : warning NU1701: Package 'System.Net.Http.Formatting.Extension 5.2.3' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'. This package may not be fully compatible with your project.
    2 Warning(s)
    0 Error(s)

Time Elapsed 00:00:09.08
Run Code Online (Sandbox Code Playgroud)

有几个问题...

  • 为什么 dotnet/nuget 允许我将此包添加到 netcoreapp2.0 项目中?
  • 为什么或如何阻止这种情况发生?

我知道有一个与 .NET Core 2.0 兼容且提供相同功能的完美包。我希望这件事失败,而且显然失败!我也知道我无法将 nuget 警告“升级”为错误(太糟糕了)。

Mat*_*ard 5

NuGet 将 .NET 4.6.1 程序集视为与 .NET Core 2.0 和 .NET Standard 2.0 兼容,但会显示一条警告,指示如果 NuGet 包使用本机 API(例如 WPF),则您的应用程序可能无法工作。

您可以将警告启用为错误,这会导致 NU1701 警告的还原失败。

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
  </PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

或者只是将 NU1701 警告标记为错误。

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <WarningsAsErrors>$(WarningsAsErrors);NU1701</WarningsAsErrors>
  </PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

不幸的是,这并不能防止dotnet add package添加 PackageReference 失败,但恢复将会失败。