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

查看:297
本文介绍了如何正确构造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>

但是,当我将此程序包安装到仍使用程序包的经典.NET 4.5或PCL项目中时。 config,然后该文件将被 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的想法不是很容易在支持软件包依赖项的所有平台上运行吗?

  1. Shouldn't this be avoided? One way to do that is to create empty dependency groups in the nuspec file 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. Should I target the whole NETStandard.Library metapackage or just the packages that I actually need? Wasn't the idea of .NET Standard / .NET Core being easily runnable on all platforms that support the dependencies of your package?

不幸的是,NuGet软件包的官方文档尚未编写使用.NET Core / .NET Standard的计算机。

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.X net45 。最初,使用 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天全站免登陆