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

标签 .net nuget .net-core

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

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

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

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

"polluted packages.config file containing all the dependencies from NETStandard.Library 1.6.0
  • 这不应该避免吗?一种方法是在 nuspec 文件 as suggested by Oren Novotny on the GitHub page of NuSpec.ReferenceGenerator 中创建空的依赖组。 .然而,他本人在 one of his recent blog posts 中不鼓励这样做。 .
  • 我应该针对整个 NETStandard.Library 元包还是只针对我实际需要的包? .NET Standard/.NET Core 的想法不是可以在支持包依赖项的所有平台上轻松运行吗?

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

    最佳答案

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

  • 在 project.json 中,在 netstandard1.X 之间拆分依赖项和 net45 .最初,使用 NETStandard.Library metapackage 使定位前者变得容易。
  • 替换 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它可以安装在任一类型的项目中,并且只提取该框架所需的内容。

    第二步,我换了NETStandard.Library使用 .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 中使用什么时。

    关于.net - 如何正确构建 netstandard1.0 NuGet 包的依赖关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38545457/

    相关文章:

    asp.net-core - 如何使用 Serilog 抑制 'An unhandled exception has occurred while executing the request.' 日志消息

    c# - 参数不适用于 Dapper.Net

    c# - 使用 protobuf-net 将自定义类序列化为原始类型

    .net - 在不同的程序集中引用相同的接口(interface)

    .net - 协议(protocol)错误|404 使用 HttpClient 或 WebClient 从 API 读取 JSON

    visual-studio - 在 Visual Studio 中向项目添加引用和添加 Nuget 包之间的区别

    nuget - nuget.config 的文档?

    c# - 禁用超时时 SqlBulkCopy 到 Azure 超时

    c# - "legacy".NET 项目是否也可以使用新的 NuGet 3 功能?

    C# 8 基本接口(interface)的默认方法调用解决方法