简体   繁体   中英

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
The Nuget Link: https://www.nuget.org/packages/csharp-extensions/1.2.0

In my project.json, I specify dnx46 and 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)

UPDATE:

This is what happens when I build the nuget package with nuget pack 在此处输入图片说明

Looking at your csharp-extensions NuGet package it looks incorrect. It has your project files and no assemblies.

I assume this was generated by running NuGet.exe package package.nuspec . This will not generate a NuGet package that can be used from NuGet.org since there are no assemblies.

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. 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. Using the .nupkg file in the artifacts directory returns the same error. 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.

I would take a look at existing NuGet packages and try to generate the same structure. Looking at System.Xml.XmlDocument for example has the new NuGet 3 style directory structure:

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

Only the directories above have the System.Xml.XmlDocument.dll in them.

NuGet 2 would have just the lib directory and does not support dotnet as a directory.

Here is the project.json configuration that eventually allowed this nuget to work with non-dnx projects:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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