简体   繁体   中英

Referencing a Portable Class Library from a .NET Core Project

Is it possible to add a reference to a PCL (targeting .NET 4.6 and .NET Core) from a .NET Core project?

I've tried creating a solution with these two project types, but if I add a reference to the PCL from the .NET Core project, it doesn't recognise any of the namespaces. It builds with no issues though.

Here are the project.json files:

PCL:

{
  "supports": {
    "net46.app": {},
    "dnxcore50.app": {}
  },
  "dependencies": {
    "Microsoft.NETCore": "5.0.0",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.0"
  },
  "frameworks": {
    "dotnet": {
      "imports": "portable-net452"
    }
  }
}

.NET Core:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },

  "frameworks": {
    "netstandard1.6": {
      "dependencies": {
        "ClassLibrary1": { // <-- Reference to the PCL
          "target": "project"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

Edit:

For what it's worth, here's the build output as requested:

Restoring NuGet packages...
To prevent NuGet from restoring packages during build, open the Visual Studio Options dialog, click on the Package Manager node and uncheck 'Allow NuGet to download missing packages during build.'
1>------ Build started: Project: ClassLibrary2, Configuration: Debug Any CPU ------
1>  C:\Program Files\dotnet\dotnet.exe build "C:\ClassLibrary2\ClassLibrary2" --configuration Debug --no-dependencies
1>  Project ClassLibrary2 (.NETStandard,Version=v1.6) will be compiled because inputs were modified
1>  Compiling ClassLibrary2 for .NETStandard,Version=v1.6
1>  Compilation succeeded.
1>      0 Warning(s)
1>      0 Error(s)
1>  Time elapsed 00:00:00.8914597
1>
========== Build: 1 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========

Edit 2:

Well I've managed to at least get it building, but now I get a runtime error about it not finding System.Runtime. Weirdly it is working in a test solution, but not the one I want to implement it on.

For reference, here are my project.json files:

PCL:

{
  "supports": {},
  "dependencies": {
    "Microsoft.CSharp": "4.0.1",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "NETStandard.Library": "1.6.0",
    "System.Runtime.Serialization.Primitives": "4.1.1"
  },
  "frameworks": {
    "net451": {},
    "netstandard1.5": {}
  }
}

.NET Core:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50",
      "dependencies": {
        "PclTest": {
          "target": "project"
        },
        "System.Runtime": "4.1.0"
      }
    }
  }
}

it doesn't recognise any of the namespaces

I believe that's because Visual Studio is confused by your combination of RC1 and RTM projects, it really compiles successfully (though I don't know if the result is actually usable).

If you want to write a library that can be consumed from .Net Core and .Net Framework, you should use netstandard , not dotnet .

The project.json of your "PCL" ClassLibrary1 should look pretty much just like the one from your ".Net Core" ClassLibrary2.

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