如何在 VS 2017 中创建具有最小依赖性的 .NET Standard NuGet 包?

How to create .NET Standard NuGet package with minimal dependencies in VS 2017?

我目前正在使用 Visual Studio 2017 迁移库项目以支持 .NET Standard 1.1。

我希望将该项目作为单个 NuGet 包发布,它可以同时针对 .NET Framework 4.5+ 和 .NET Core、UWP 等

但是,当我尝试在 .NET Framework 项目中安装生成的包时,会生成一个巨大的包依赖项列表,其中包含 .NET 标准中定义的所有包(见下文):

我知道这些都是定义为 .NET Standard 1.1 规范一部分的程序集。然而,我的特定项目实际上只需要其中的一小部分,这个依赖列表对于在他们的项目中安装包的任何人来说都会非常混乱。

我尝试遵循 的答案,其中的建议是更改项目规范以仅引用项目所需的确切依赖项。

但是,答案是在旧 project.json 格式的上下文中,它现在已被 VS 2017 中的新 .csproj 格式所取代。我试图删除对 .NET Standard 1.1 的依赖通过删除 <TargetFramework> 指令来添加元数据包,但我只是设法破坏了构建,找不到任何方法来专门只添加所需的依赖项。

将库迁移到 .NET Standard 以实现最大平台兼容性的承诺非常有吸引力,但是推荐的构建依赖关系的方法是什么,这样针对 "classic" .NET Framework 的项目找不到他们的项目"polluted" 所有这些依赖关系?

<TargetFramework>更改为<TargetFrameworks>并添加;net45。您仍然可以获得单个 NuGet 包输出,但现在如果您的目标是 .NET 核心应用程序(它已经具有依赖项),它只会引入额外的依赖项。

如果您查看 net45 的那些包引用,您会发现没有要加载的实际 DLL。包在那里,以便像 coreclr 这样的运行时将 BCL 的所有部分加载为 dll's 可以得到它们。但是在 net45 中,您实际上不会在这些包中找到任何 dll。

简而言之,在 .net 4.x 中,.net 仍将使用 GAC 来加载这些程序集。

首先,我想 point out 这主要是一个设计时问题,不会影响生成的应用程序和库的可移植性:

In the past, we've given developers the recommendation to not reference the meta package (NETStandard.Library) from NuGet packages but instead reference individual packages, like System.Runtime and System.Collections. The rationale was that we thought of the meta package as a shorthand for a bunch of packages that were the actual atomic building blocks of the .NET platform. The assumption was: we might end up creating another .NET platform that only supports some of these atomic blocks but not all of > them. There were also concerns regarding how our tooling deals with large package graphs.

Moving forward, we'll simplify this:

  1. .NET Standard is an atomic building block. In other words, new platforms aren't allowed to subset .NET Standard -- they have to implement all of it.

  2. We're moving away from using packages to describe our platforms, including .NET Standard.

This means, you'll not have to reference any NuGet packages for .NET Standard anymore. You expressed your dependency with the lib folder, which is exactly how it has worked for all other .NET platforms, in particular .NET Framework.

However, right now our tooling will still burn in the reference to NETStandard.Library. There is no harm in that either, it will just become redundant moving forward.

但是,我完全承认,这样的结果很难让人满意。

使用 .NET Standard 1.x 和 packages.config,很遗憾,您别无选择,只能生成带有自定义依赖项组的手写 .nuspec。然而,这需要了解哪些包是必需的并且往往是脆弱的。如果消费者使用 <PackageReference> 就不会那么头疼了,因为它将直接引用与传递引用分开了,因此这些在你面前并不那么重要。

有了 .NET Standard 2.0,这个问题将完全消失。