3

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.

3
  • Forget everything about that and start from scratch in Visual Studio 2017. Don't waste your precious time. Commented Feb 24, 2017 at 1:11
  • 1
    @LexLi It's still good to know what's happening under the hood though :) Commented Feb 24, 2017 at 7:04
  • @goaty Yes, especially if you are intending to write a library that is aim to be used by different framework as I wrote in my answer Commented Feb 24, 2017 at 8:26

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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 :)

5 Comments

Read again my answer. I did not say the contrary. I am giving a full explanation of Web API using a library for .NET Standard ;-) However netcoreapp1.0 is not only for .NET Core console apps. That framework enables you to run MVC on the top.
I still don't understand then, if my Class Library is a .NETStandard 1.6 why can't I load it as a reference to a .NETCoreApp?
@Steve'Kepano'Eggering Your API can use your class library. You have 2 ways to do it. First way: make your class library a package and reference it. Second way: if don't want to make a package, then your only solution is to "include" the class library project into your API's Visual Studio Solution. I updated my answer.
@GuillaumeS. I understand both those options, and appreciate the input. Making a nuget package might have to be my solution for now, but I would still like to understand the process of using a .NETStandard dll as a reference in a .NETCoreApp, and why this isn't working for me. This particular library is going to be used across many applications. so the second option doesn't seem as lucrative.
@Steve'Kepano'Eggering Read my updated answer (not my comments). There is a link to the Microsoft documentation that can help you to understand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.