简体   繁体   中英

How can I use .Net Core Class Library (.NetStandard v1.5) into .Net Framework 4.6.2 Application

According to Mapping the .NET Platform Standard to platforms .NET Platform Standard 1.5 have to be compatible with .NET Framework 4.6.2. I have tried to use it (make new .NET Platform Standard class library, then new .Net Framework 4.6.2 console application), but the library is not recognized. What I am doing wrong?

project.json in class library:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.5.0-rc2-24027"
  },

    "frameworks": {
        "netstandard1.5": {
            "imports": "dnxcore50",
            "buildOptions": { "embed": "true" }
        }
    }
}

If you get the latest .net core RTM release and the Update 3 VS tooling, things are a little nicer than they were during the RC period. Still don't have easy project references from csproj referencing xproj - but, you can get things to talk by packing up the xproj output into a nuget package, then using the package manager to install that package. Also, you shouldn't need the imports on the framework, nor the dependency on netstandard.library, at least I don't. Here's how I've done it:

  • Create a .cmd file which will package the nuget files and two copy the output files to the folder where the package manager is expecting them. Here's that script, I call it makeNuget.cmd:

    IF "%1" == "%2" (

    dotnet pack --no-build --configuration %1 -o ../%3

    xcopy %4 "...\\%3\\lib\\%5\\" /Y

    )

  • Add a postbuild script in the project.json of the xproj to run the script

    "scripts": {
    "postcompile": [ "makeNuget.cmd %compile:Configuration% Release packages\\%project:Name% %compile:OutputDir% %compile:TargetFramework%"
    ] }

This should leave you with a nuget package in the packages\\[projectName]\\ folder at the root of your solution, with the binaries in the packages\\[projectName]\\lib\\[targetFramwork]\\ folder

  • Now, you need to add the packages folder as a package source, so first open the package manager console, then click the little gear to add a package source (or Ctrl+Q, package sources, Enter). Then click the add button, name this source, browse or type in the packages directory and hit ok. 在此处输入图片说明
  • In the package manager console, make sure both your package source and project are selected in the drop downs at the top.
  • install-package [your package name]

AFAIK, that's as good as it gets at the moment.

Do you really need .Net Framework 4.6.2 app?

I just created PCL (Portable class library) targeting .NETStandard1.4 and I can use it in WPF app which is targeting .NET Framework 4.6.1. Here is how my project.json looks like in PCL project:

{
  "supports": {},
  "dependencies": {
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "NETStandard.Library": "1.6.0"
  },
  "frameworks": {
    "netstandard1.4": {}
  }
}

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