如何使 nuget 包正确定位多个框架?

How do I make a nuget package target multiple frameworks correctly?

目前,我收到以下错误:

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.          

这是我的回购 link:https://github.com/NullVoxPopuli/csharp-extensions
Nuget Link: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 安装到的项目是一个 Class 库,目标是 .NET Framework 4.6(不是 dnx 项目)

更新:

这是我使用 nuget pack 构建 nuget 包时发生的情况

查看您的 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 文件 returns 同样的错误。您似乎只能将此工件 .nupkg 安装到 DNX 项目(控制台或 Web 应用程序)中,但不能安装到 DNX Class 库项目中。

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

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

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

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

这是最终允许此 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",
},