如何正确构建 netstandard1.0 NuGet 包的依赖关系? [英] How to properly structure the dependencies of a netstandard1.0 NuGet package?

查看:20
本文介绍了如何正确构建 netstandard1.0 NuGet 包的依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I updated a PCL with profile 259 to .NET Standard 1.0 and want to update the corresponding NuGet package accordingly. I changed the folder containing the actual DLL from portable-net45+win8+wp8+wpa81 to netstandard1.0, but I am not quite sure how to structure the dependencies of the package.

如果我使用 .NET Core CLI 创建包 (dotnet pack),nuspec 文件中的依赖项部分看起来像这样:

If I use the .NET Core CLI to create a package (dotnet pack), the dependencies section in the nuspec file simply looks like this:

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

但是,当我将此包安装到仍然使用 packages.config 的经典 .NET 4.5 或 PCL 项目时,此文件会被 NETStandard.Library 元包中的所有依赖项污染",像这样:

However, when I install this package to a classic .NET 4.5 or PCL project that still uses packages.config, then this file gets "polluted" with all the dependencies from the NETStandard.Library metapackage, like so:

  1. 这不应该避免吗?一种方法是在 nuspec 文件中创建空的依赖组">正如 Oren Novotny 在 NuSpec.ReferenceGenerator 的 GitHub 页面上所建议的.然而,他本人在 他最近的一个博文.
  2. 我应该针对整个 NETStandard.Library 元包还是只针对我实际需要的包?.NET Standard/.NET Core 的想法是不是可以在支持包依赖项的所有平台上轻松运行?

不幸的是,官方文档尚未编写用于带有 .NET Core/.NET Standard 的 NuGet 包.

Unfortunately, the official documentation for NuGet packages with .NET Core / .NET Standard is not written yet.

推荐答案

对于我维护的面向 .NET Core 和 .NET 4.5 的包,我不得不解决这个问题.我使用的方法涉及您问题的这两点:

I've had to address this problem for packages I maintain that target both .NET Core and .NET 4.5. The approach I use touches on both points of your question:

  1. 在 project.json 中,拆分 netstandard1.Xnet45 之间的依赖项.最初,使用 NETStandard.Library 元包可以轻松定位前者.
  2. NETStandard.Library 替换为对我实际需要的特定包的引用.
  1. In project.json, split the dependencies up between netstandard1.X and net45. Initially, use the NETStandard.Library metapackage to make targeting the former easy.
  2. Replace NETStandard.Library with references to the specific packages I actually need.

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

In the first step, my project.json looks something like this:

{
   "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 依赖项则根据需要放在各自的部分中.

Any dependencies that themselves are already compatible with either framework go in dependencies, while specific .NET Core or .NET 4.5 dependencies go in their respective sections as needed.

使用 dotnet pack,这正是我所需要的:一个 .nupkg 可以安装在任何类型的项目中,并且只提取它需要的东西框架.

With dotnet pack, this produces exactly what I need: a single .nupkg that can be installed in either type of project and pulls in only what it needs for that framework.

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

In the second step, I swapped out NETStandard.Library with the few packages I actually needed for .NET Core:

{
   "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 中使用什么时.

This second step isn't necessary, but it's nice to produce a package with minimal dependencies for both platforms. NETStandard.Library is useful in the development phase, when you're not quite sure what you'll need to use in the core API.

这篇关于如何正确构建 netstandard1.0 NuGet 包的依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆