Why does the .NetCore Class Library reference .NetStandard?

681 views Asked by At

In VS2015 > New Project > .Net Core, there is a template for "Class Library (.NET Core)"

It wasn't until I tried to reference this library in a .NET Core Web API program, that I realized that the Class Library template is referencing .NETStandard v1.6. And my .NET Core API porject won't take it as a reference. I was trying to avoid building the library as a nuget package.

Any ideas on why a Core template isn't referencing core? Any ideas on quick workarounds?

Update: Opened a new "Class Library (.NET Core)", before first build I changed frameworks in project.json to

"netcoreapp1.0": { "imports": [ "dotnet5.6", "portable-net45+win8" ]

Then I created, in a different VS instance, a new "ASP.NET Core Web Application (.NET Core)".

I tried to add a reference to the .dll file for the new class library, now in a netcoreapp1.0 folder and I still get the same error:

.NET Core Projects only support referencing .NET framework assemblies in this release.

2

There are 2 answers

0
Guillaume S. On BEST ANSWER

How to fix your problem

I was trying to avoid building the library as a nuget package.

Any ideas on why a Core template isn't referencing core? Any ideas on quick workarounds?

When you try to add the .dll as a reference for the API, your error message should say somewhere that you need to create a package in order to use that class library. It is described in the documentation.

However if you do not want to bother to make a package, for example if your class library is used now only in your API project, the easiest way is to build your class library as a Project of your Solution where the API lives. To do so, copy-paste your code in the Solution folder and include it as a project.

Some explanations about the version and frameworks

The Core template (I guess you are talking about the .NET Core API) should create a project.json with

  "frameworks": {
    "netcoreapp1.0": {
         "imports": [
             "dotnet5.6",
             "portable-net45+win8"
       ]
    }
  }

Let's see what the lines are responsible for:

  • netcoreapp1.0 under frameworks means your API is targeting the framework .NET Core 1.0. That version of the .NET Core framework implements the .NET Standard 1.6 (the list here) Good thing: your Class library also use this .NET Standard 1.6. Knowing the .NET Standard 1.6 that your Class library is using make you able to choose what version of .NET Core/Xamarin/.NET Framework you can use when you later want to develop a Web app/Mobile app/Windows software for example
  • imports lists other frameworks/version of packages that your application can use
  • According to this link dotnet5.6 and portable-net45+win8 are deprecated and a netstandard version should be used instead. dotnet5.6 is still mysterious for me. portable-net45+win8 is equivalent to netstandard1.0. If you let it it means that your application can use package using .NET Standard 1.0. This can be a problem because .NET Core 1.0 starts with .NET Standard 1.6 so I would actually delete those two lines.

That being said, I am more than welcome for comments!

5
AudioBubble On

It seems like other answers (before this) didn't get to the point. .NET Standard is the way forward to write .NET class libraries because it follows a set of standard APIs that can run on virtually all .NET runtimes (full framework, .NET core, Mono Xamarin...)

So if you want to write a .NET core library, you're effectively writing a .NET Standard library. No need to differentiate them. The reference to .NET standard 1.6 is to let the compiler know what to compile against. netcoreapp1.0 is basically for .NET core console apps. A netcoreapp1.0 project will compile to dll that can be executed (contains program entry like static void Main())

So it makes sense why you can't have that in a library :)