如何正确构建 netstandard1.0 NuGet 包的依赖关系?

How to properly structure the dependencies of a netstandard1.0 NuGet package?

我将配置文件 259 的 PCL 更新为 .NET Standard 1.0,并想相应地更新相应的 NuGet 包。我将包含实际 DLL 的文件夹从 portable-net45+win8+wp8+wpa81 更改为 netstandard1.0,但我不太确定如何构建包的依赖关系

如果我使用 .NET Core CLI 创建包 (dotnet pack),nuspec 文件中的依赖项部分如下所示:

<dependencies>
  <group targetFramework="netstandard1.0">
    <dependency id="NETStandard.Library" version="1.6.0" />
  </group>
</dependencies>

但是,当我将这个包安装到经典的 .NET 4.5 或 PCL 项目时,它仍然使用 packages.config,然后这个文件得到 "polluted" 以及来自 NETStandard.Library 元包,像这样:

  1. 不应该避免这种情况吗?一种方法是在 nuspec 文件 as suggested by Oren Novotny on the GitHub page of NuSpec.ReferenceGenerator. However, he himself discourages this in one of his recent blog posts.
  2. 中创建空的依赖组
  3. 我应该针对整个 NETStandard.Library 元包还是只针对我实际需要的包? .NET Standard / .NET Core 的想法不是可以在支持包依赖项的所有平台上轻松运行吗?

不幸的是,尚未编写 .NET Core / .NET Standard 的 NuGet 包的official documentation

我必须为我维护的同时针对 .NET Core 和 .NET 4.5 的包解决这个问题。我使用的方法涉及你问题的两点:

  1. 在 project.json 中,拆分 netstandard1.Xnet45 之间的依赖关系。最初,使用 NETStandard.Library 元数据包来轻松定位前者。
  2. NETStandard.Library 替换为对我实际需要的特定包的引用。

在第一步中,我的 project.json 看起来像这样:

{
   "dependencies": {
      "MyOtherLibrary": "1.0.0"
   },
   "frameworks": {
      "net45": {
         "frameworkAssemblies": {
            "System.Collections":"4.0.0.0"
         }
      },
      "netstandard1.3": {
         "dependencies": {
            "NETStandard.Library": "1.6.0"
         }
      }
   }
}

本身已经与任一框架兼容的任何依赖项进入 dependencies,而特定的 .NET Core 或 .NET 4.5 依赖项根据需要进入各自的部分。

使用 dotnet pack,这会产生我所需要的:一个 .nupkg 可以安装在任一类型的项目中,并且只引入该框架所需的内容。

在第二步中,我用 .NET Core 实际需要的几个包替换了 NETStandard.Library

{
   "dependencies": {
      "MyOtherLibrary": "1.0.0"
   },
   "frameworks": {
      "net45": {
         "frameworkAssemblies": {
            "System.Collections":"4.0.0.0"
         }
      },
      "netstandard1.3": {
         "dependencies": {
            "System.Threading.Tasks": "4.0.11",
            "System.Net.Http": "4.1.0"
         }
      }
   }
}

这第二步不是必需的,但最好为两个平台生成一个依赖性最小的包。 NETStandard.Library 在开发阶段很有用,当你不太确定你需要在核心中使用什么时 API。