如何避免与NuGet的兼容性警告?

时间:2018-03-13 20:39:52

标签: .net-core nuget

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

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
</Project>

我从包系统中获得了一些不那么令人敬畏的行为。我被允许添加this package。我可能没有看到它只与.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'.

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

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

有几个问题......

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

我知道有a perfectly good package与.NET Core 2.0兼容,提供相同的功能。我希望这失败并且显然失败!我也知道我不能将一个nuget警告“升级”为错误(太糟糕了)。

1 个答案:

答案 0 :(得分:3)

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>

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

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

不幸的是,这并不妨碍dotnet add package无法添加PackageReference,但恢复将失败。