简体   繁体   English

如何使nuget包正确地针对多个框架?

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

Currently, I get the following error: 当前,我收到以下错误:

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.          

Here is a link to my repo: https://github.com/NullVoxPopuli/csharp-extensions 这是我的仓库的链接: https : //github.com/NullVoxPopuli/csharp-extensions
The Nuget Link: https://www.nuget.org/packages/csharp-extensions/1.2.0 Nuget链接: https ://www.nuget.org/packages/csharp-extensions/1.2.0

In my project.json, I specify dnx46 and dnxcore50 在我的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,]"
            }
        }
    },
}

The project I'm trying to install the nuget into is a Class Library targeting .NET Framework 4.6 (and isn't a dnx project) 我试图将nuget安装到的项目是一个针对.NET Framework 4.6的类库(并且不是dnx项目)

UPDATE: 更新:

This is what happens when I build the nuget package with nuget pack 这是我用nuget pack构建nuget包时发生的情况 在此处输入图片说明

Looking at your csharp-extensions NuGet package it looks incorrect. 查看您的csharp-extensions NuGet软件包,它看起来不正确。 It has your project files and no assemblies. 它具有您的项目文件,而没有程序集。

I assume this was generated by running NuGet.exe package package.nuspec . 我假设这是通过运行NuGet.exe package package.nuspec生成的。 This will not generate a NuGet package that can be used from NuGet.org since there are no assemblies. 由于没有程序集,因此不会生成可从NuGet.org使用的NuGet程序包。

If you build your .xproj in Visual Studio it will generate a .nupkg in the artifacts directory for you if you have Product outputs on build checked in the project options, which you have. 如果在Visual Studio中构建.xproj,并且在项目选项中选中了“ Product outputs on build它将在工件目录中生成.nupkg。 Unfortunately this .nupkg file, whilst it has assemblies, only has a lib\\dnxcore50 directory and NuGet will not allow you to install it into a .NET 4.6 project since NuGet finds them to be incompatible. 不幸的是,这个.nupkg文件虽然具有程序集,但只有一个lib \\ dnxcore50目录,并且NuGet不允许您将其安装到.NET 4.6项目中,因为NuGet发现它们不兼容。 Using the .nupkg file in the artifacts directory returns the same error. 在工件目录中使用.nupkg文件将返回相同的错误。 You only seem to be able to install this artifact .nupkg into a DNX project (Console or Web app) but not a DNX Class Library project. 您似乎只能将此工件.nupkg安装到DNX项目(控制台或Web应用程序)中,但不能安装到DNX类库项目中。

I would take a look at existing NuGet packages and try to generate the same structure. 我将看一下现有的NuGet软件包,并尝试生成相同的结构。 Looking at System.Xml.XmlDocument for example has the new NuGet 3 style directory structure: 例如,以System.Xml.XmlDocument为例,具有新的NuGet 3样式目录结构:

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

Only the directories above have the System.Xml.XmlDocument.dll in them. 仅以上目录中包含System.Xml.XmlDocument.dll。

NuGet 2 would have just the lib directory and does not support dotnet as a directory. NuGet 2将只有lib目录,不支持dotnet作为目录。

Here is the project.json configuration that eventually allowed this nuget to work with non-dnx projects: 这是project.json配置,该配置最终允许此nuget与非dnx项目一起使用:

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

the key parts were (under frameworks) 关键部分是(在框架下)

 "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-*",
            }

        },

and

using the generic test runners for xunit: 对xunit使用通用测试运行程序:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM