【问题标题】:Add .NET Core support to existing class library将 .NET Core 支持添加到现有类库
【发布时间】:2016-10-06 04:38:53
【问题描述】:

我有一个用 .NET 4.5 编写的现有开源库,我想从中创建一个 NuGet。问题是这个 NuGet 包只能用于 .NET 4.5 应用程序。

我想要做的是在同一个 NuGet 上同时支持 .NET 4.5 和 .NET Core(我已经看到了执行 JSON.NET 行的包) - 如何向现有 NuGet 添加支持?以及如何在单个类库中支持多个 .NET 版本?

【问题讨论】:

  • 除非您的用户有强烈的需求,否则请稍候。这就是我为我的开源项目所做的。当前的工具太痛苦了,您可能不想浪费时间。微软最近做出了一些承诺,blogs.msdn.microsoft.com/dotnet/2016/09/26/…
  • @LexLi 谢谢。您能否详细说明目前可用的工具有哪些?
  • dot.net 拥有您所需要的一切。

标签: .net .net-core asp.net-core-1.0


【解决方案1】:

您可以继续将 csproj 用于 net45 目标并添加同时针对“net45”和“netstandard1.x”框架的 project.json(使用 project.json of my library 作为示例):

  "frameworks": {
    "net45": {
      "frameworkAssemblies": {
        "System.Data": "",
        "System.ComponentModel.DataAnnotations" : ""
      },
      "buildOptions": {
        "define": []
      }
    },
    "netstandard1.5": {
      "dependencies": {
        "NETStandard.Library": "1.6.0",
        "System.Data.Common": "4.1.0",
        "System.Reflection": "4.1.0",
        "System.Reflection.Primitives": "4.0.1",
        "System.Threading": "4.0.11",
        "System.Threading.Tasks": "4.0.11",
        "System.ComponentModel.Annotations": "4.1.0" 
      },
      "buildOptions": {
        "define": [ "NET_STANDARD" ]
      }
    }

请注意,您可以为 net45 或 netcore 特定代码 sn-ps 定义条件编译常量。

当您拥有 project.json 后,您可以使用以下命令准备包含 net45 和 netstandard 构建的 nuget 包:

> dotnet pack --configuration Release

不要忘记“dotnet pack”不使用来自 nuspec 文件的信息,所有元数据都应该存在于 project.json 中。

【讨论】:

    猜你喜欢
    • 2017-11-09
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多