方案 1
不会修剪 PackageReference System.Text.Json。 此包自动可用,无需显式引用。 删除 PackageReference 项
Issue
由于 NuGet 依赖项关系图修剪而引发此警告,并指示由于直接 PackageReference而还原了其他可收缩包。
如果删除了直接 PackageReference ,则可以修剪命名包,因为目标 .NET SDK 提供此程序集的相同版本或更高版本。
此警告仅影响通过 PrunePackageReference 功能进行修剪的包。
仅当问题PackageReference时,才会引发此问题。
示例 1
当目标 .NET SDK 包含等效版本时, 依赖项冲突解决 将选择 SDK 捆绑程序集:
<PropertyGroup>
<!-- 'System.Text.Json' is SDK-bundled in 'net10.0' -->
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<!-- The assembly provided by this reference will not be used -->
<PackageReference Include="System.Text.Json" Version="10.0.0" />
</ItemGroup>
示例 2
当目标 .NET SDK 包含多个框架目标的等效版本时, 依赖项冲突解决 会为每个目标选择适当的 SDK 捆绑程序集:
<PropertyGroup>
<!-- 'System.Text.Json' is SDK-bundled in both TFMs -->
<TargetFrameworks>net9.0;net10.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<!-- Neither of the assemblies provided by these references will be used -->
<PackageReference Include="System.Text.Json" Version="10.0.0" Condition="'$(TargetFramework)' == 'net10.0'"/>
<PackageReference Include="System.Text.Json" Version="9.0.4" Condition="'$(TargetFramework)' == 'net9.0'"/>
</ItemGroup>
Solution
删除不必要的 PackageReference。
方案 2
PackageReference Microsoft。不会修剪 Extensions.Caching.Memory。 此包自动可用,无需显式引用。 删除 PackageReference 项
Issue
用于修剪的包列表由当前项目的直接 FrameworkReference 项确定。
FrameworkReference 项是可传递的。
但是,当当前项目通过 as ProjectReference继承框架引用时,修剪不会使用该可传递框架引用来删除包。
请注意,生成时冲突解决将删除此包。
Example
在此示例中,Library.csproj 引用Microsoft.AspNetCore.App,Consumer.csproj引用Library.csproj。
如果没有直接 FrameworkReference 传入 Consumer.csproj,NuGet 无法确定包是可修剪的。
因此,使用者项目中不会将 Microsoft.Extensions.Caching.Memory视为可修剪。
<!-- Library.csproj -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
<!-- Consumer.csproj -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Library\Library.csproj" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
</ItemGroup>
</Project>
直接 FrameworkReference 添加到 Consumer.csproj.
然后,NuGet 可以确定共享框架提供 Microsoft.Extensions.Caching.Memory。
它为不必要的直接包引用引发 NU1510。
<ItemGroup>
<ProjectReference Include="..\Library\Library.csproj" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
</ItemGroup>
Solution
将匹配 FrameworkReference 直接添加到当前项目。
然后删除不必要的 PackageReference。
Note
从 .NET 10 开始, PrunePackageReference 默认为所有面向 .NET 10 或更高版本的项目启用该功能。 仅当修剪应用于所有运行时目标时,才会引发此警告:
<PropertyGroup>
<!-- 'System.Text.Json' is not SDK-bundled in 'net48' -->
<TargetFrameworks>net10.0;net48</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<!-- This reference is needed in 'net48' - NU1510 is not raised -->
<PackageReference Include="System.Text.Json" Version="9.0.7" />
</ItemGroup>