A common class library consumed by both .NET Core and .Net 4.5.2
Asked Answered
S

2

13

I'm fairly new to .Net Core, but have made a working Asp.Net Core WebAPI site - now I want to share some code with another project...

  • I have Visual Studio 2015 with Update 3 installed.
  • I have DotNetCore.1.0.0-VS2015Tools.Preview2.exe installed from here.

I would like to create a shared library (PCL) that can be consumed by two other libraries - it only contains primitive classes/interfaces with no other dependencies. One of the consuming libraries is a new vanilla project targeting "netstandard1.6", the other is an old client library which targets .Net 4.5.2 (I can upgrade this to 4.6.x if I must).

I've been round in circles, and I can't make the netstandard1.6 library reference the PCL - I just get told the types are missing:

Error CS0246: The type or namespace name 'SomeTypeHere' could not be found (are you missing a using directive or an assembly reference?)

The PCL named "ClassLibrary1"'s project.json is auto-generated as:

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

My consuming library project.json is:

{
  "version": "1.0.0-*",
  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "Newtonsoft.Json": "9.0.1"
  },
  "frameworks": {
    "netstandard1.6": {
      "dependencies": {
        "ClassLibrary1": {
          "target": "project"
        }
      }
    }
  }
}  

How can I make this work?

EDIT 07/07/2016:

I have made the following solution available, which demonstrates my setup: https://github.com/JonnyWideFoot/netcore-prototype See ExperimentClient::GetLocationAsync for where I would like to use the Contracts Library within the .Net 4.5.2 / 4.6.x Client.

Smarten answered 6/7, 2016 at 15:46 Comment(1)
FYI - they're not called PCLs anymore with the advent of .NET Core :)Roselleroselyn
S
1

The only way I have found to make this work, is to hack reference the .csproj file of the Client library: https://github.com/JonnyWideFoot/netcore-prototype/blob/master/src/JE.API.Experiment.Client/JE.API.Experiment.Client.csproj

<Reference Include="JE.Api.Experiment.Contract, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>..\JE.Api.Experiment.Contract\bin\$(Configuration)\net452\JE.Api.Experiment.Contract.dll</HintPath> </Reference>

By hard-coding the path to the correct output folder from the contracts library, all is fine.

... thinking this could be a bug in visual studio.

Smarten answered 18/7, 2016 at 18:27 Comment(0)
R
4

Here's how I create shared libraries that can be consumed from both .NET Core projects and .NET 4.5 projects:

SharedLibrary\project.json

"dependencies": { },
"frameworks": {
  "net45": { },
  "netstandard1.1": {
    "dependencies": {
      "NETStandard.Library": "1.6.0"
    }
  }
},
"version": "1.0.0"

A consuming (.NET Core) library in the same solution references it like this:

"dependencies": {
  "SharedLibrary": {
    "target": "project",
    "version": "1.0.0"
  }
},
"frameworks": {
  "netstandard1.1": { }
  }
}

A consuming .NET 4.5 project using project.json would look the same with the exception of net45 in the frameworks section. Installing in a csproj-based .NET 4.5 project works too, if a NuGet package for SharedLibrary is produced.

According to the .NET Platform Standard docs, simply targeting netstandard1.1 should allow the shared library to be installed in .NET 4.5+ projects as well. I've run into strange issues with that, but it may have been the result of beta tooling.

Roselleroselyn answered 6/7, 2016 at 16:37 Comment(6)
Thanks! - Ive got the 'netstandard side' working now, but still having issues with referencing the Shared project from the .Net 4.5.2 client. { "dependencies": {}, "frameworks": { "net452": {} }, "version": "1.0.0" } I reference the project in visual studio (both libraries are in the same solution), and I get "error CS0234". However, the shared library HAS produced a .Net 4.5.2 dll. weirdly if I reference that directly, everything works.Smarten
When referencing the project rather than the DLL, the hintpath in the properties window is correct: <src_path>\ClientLib\bin\Debug\net452\SharedLib.dll All very Weird... How have M$ made this stuff SO complicated. .Net used to be simple :-(Smarten
@JonRea: To clarify - are you referencing the shared project from a .NET 4.5.2 project that's based on project.json or based on csproj?Roselleroselyn
"are you referencing the shared project from a .NET 4.5.2 project" - yes ... "based on project.json or based on csproj" -- it depends on which VS template I select. Ive tried with both xproj and csproj, both having a project.json.Smarten
I have edited the question to add an example solution.Smarten
@JonRea I can't see any option to create a project with the project.jsonconfig. Have they gone back to the .csproj format?Virgo
S
1

The only way I have found to make this work, is to hack reference the .csproj file of the Client library: https://github.com/JonnyWideFoot/netcore-prototype/blob/master/src/JE.API.Experiment.Client/JE.API.Experiment.Client.csproj

<Reference Include="JE.Api.Experiment.Contract, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>..\JE.Api.Experiment.Contract\bin\$(Configuration)\net452\JE.Api.Experiment.Contract.dll</HintPath> </Reference>

By hard-coding the path to the correct output folder from the contracts library, all is fine.

... thinking this could be a bug in visual studio.

Smarten answered 18/7, 2016 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.