【问题标题】:How do I make a nuget package target multiple frameworks correctly?如何使 nuget 包正确地针对多个框架?
【发布时间】:2016-05-18 14:40:02
【问题描述】:

目前,我收到以下错误:

Could not install package 'csharp-extensions 1.2.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.          

这是我的仓库的链接:https://github.com/NullVoxPopuli/csharp-extensions
Nuget 链接:https://www.nuget.org/packages/csharp-extensions/1.2.0

在我的 project.json 中,我指定了 dnx46 和 dnxcore50

{
    "version": "1.2.0",
    "configurations": {
        "Debug": {
            "compilationOptions": {
                "define": [ "DEBUG", "TRACE" ]
            }
        },
        "Release": {
            "compilationOptions": {
                "define": [ "RELEASE", "TRACE" ],
                "optimize": true
            }
        }
    },
    "dependencies": {
        "Microsoft.Extensions.PlatformAbstractions": "1.0.0-*",
        "System.Reflection": "4.1.0-*",
        "xunit": "2.1.0-*",
        "xunit.runner.dnx": "2.1.0-*"
    },
    "commands": {
        "run": "csharp_extensions",
        "test": "xunit.runner.dnx"
    },
    "frameworks": {
        "dnx46": {
            "dependencies": {
                "System.Console": "4.0.0-beta-*",
                "System.Reflection.TypeExtensions": "4.1.0-beta-*",
                "System.Runtime.Extensions": "4.0.11-beta-*",
                "System.Dynamic.Runtime": "4.0.11-beta-*",
                "Microsoft.CSharp": "4.0.1-beta-*",
                "System.IO": "4.0.11-beta-*"
            }
        },
        "dnxcore50": {
            "_": "this is the recommended windows runtime",
            "dependencies": {
                "System.Console": "4.0.0-beta-*",
                "System.Reflection.TypeExtensions": "4.1.0-beta-*",
                "System.Runtime.Extensions": "(4.0,]",
                "System.Dynamic.Runtime": "(4.0.0,]",
                "Microsoft.CSharp": "(4.0.0,]",
                "System.IO": "(4.0,]"
            }
        }
    },
}

我尝试将 nuget 安装到的项目是面向 .NET Framework 4.6 的类库(并且不是 dnx 项目)

更新:

当我使用 nuget pack 构建 nuget 包时会发生这种情况

【问题讨论】:

    标签: c# .net nuget class-library dnx


    【解决方案1】:

    查看您的 csharp-extensions NuGet 包,它看起来不正确。它有你的项目文件,没有程序集。

    我假设这是通过运行NuGet.exe package package.nuspec 生成的。这不会生成可以从 NuGet.org 使用的 NuGet 包,因为没有程序集。

    如果您在 Visual Studio 中构建 .xproj,如果您在项目选项中选中了 Product outputs on build,它将在工件目录中为您生成一个 .nupkg。不幸的是,这个 .nupkg 文件虽然有程序集,但只有一个 lib\dnxcore50 目录,并且 NuGet 不允许您将其安装到 .NET 4.6 项目中,因为 NuGet 发现它们不兼容。使用 artifacts 目录中的 .nupkg 文件会返回相同的错误。您似乎只能将此工件 .nupkg 安装到 DNX 项目(控制台或 Web 应用程序)中,但不能安装到 DNX 类库项目中。

    我会查看现有的 NuGet 包并尝试生成相同的结构。以 System.Xml.XmlDocument 为例,它具有新的 NuGet 3 样式目录结构:

    \lib
        \dotnet
        \net46
    \ref
        \dotnet
        \net46
    

    只有上面的目录有 System.Xml.XmlDocument.dll。

    NuGet 2 将只有 lib 目录,不支持将 dotnet 作为目录。

    【讨论】:

    • 我一直在使用nuget pack 命令来生成.nupkg——它没有在lib 文件夹中放任何东西——它应该是自动的吗?我在构建时确实收到警告:(请参阅有问题的更新)。另外,如何构建非 dnxcore50 dll?
    • 即使我手动创建了一个 lib/dnx46 文件夹并将 csharp-extensions.dll 拖入其中,我仍然会在屏幕截图中收到该警告...
    • 它将程序集放入 lib 文件夹。但是,您需要在 .nuspec 中明确包含它们,或者将文件夹放在 .nuspec 上方,以便 NuGet 使用它们。请参阅Creating a NuGet package 文档。
    • 是否有不同的指南?我看过那个,但我不明白如何将它所说的应用到我的项目中
    • 我不知道。我通常会明确列出 .nuspec 文件中的文件。这是一个.nuspec。不需要添加源文件。添加它们允许您生成符号包,以便其他人可以调试您的 NuGet 包。
    【解决方案2】:

    这是最终允许此 nuget 与非 dnx 项目一起使用的 project.json 配置:

    https://github.com/NullVoxPopuli/csharp-extensions/blob/2767e8ae87dcc69a14d1a33fd63b9eef364ee1ec/project.json

    关键部分是(在框架下)

     "net46": {
                "frameworkAssemblies": {
                    "System.Runtime": {
                        "type": "build",
                        "version": ""
                    }
                },
                "dependencies": {
                    "Microsoft.CSharp": "4.0.1-beta-*",
                    "System.Dynamic.Runtime": "4.0.11-beta-*",
                    "System.IO": "4.0.11-beta-*",
                    "System.Reflection.TypeExtensions": "4.1.0-beta-*",
                    "System.Runtime.Extensions": "4.0.11-beta-*",
                }
    
            },
    

    使用 xunit 的通用测试运行器:

    "dependencies": {
        "Microsoft.Extensions.PlatformAbstractions": "1.0.0-*",
        "System.Reflection": "4.1.0-*",
        "xunit": "2.1.0-*",
        "xunit.runners": "2.0.0",
    },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多