adding class library to web application with net core - c#

I'v created a web application with net core and with this project
"frameworks": {
"net461": {
"dependencies": {
"Core": {
"target": "project"
}
}
}
},
Then I've crated a class library net core with this project
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
but when I try to add the class project to web application, VS2015 tell me
The following project are not supported as reference.
WebApplication:
.NETFramework, Version=v4.6.1
ClassLibrary:
.NETStandard, Version=v1.6
How can I add class library to main project?

If you want to target .NET framework 4.6.1, your class library must be .NET Standard 1.4 or lower as suggested on official docs - How to target the .NET Standard
1) Create .NET core class library. project.json looks like this-
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.4": {
"imports": "dnxcore50"
}
}
}
2) In ASP.NET Core application (.NET framework), it can reference like below-
"frameworks": {
"net461": {
"dependencies": {
"ClassLibrary1": {
"target": "project"
}
}
}
},

You have to start your project as a dotnetcore project (see image below) because it seems like you want to add a framework 4.6 file to your dotnetcore application.
for more information about dotnetcore compatibulity check this post
Compatibility between dotnetcore and framework 4.5

Related

.NETStandard Library nuget package - cannot install on standard project

I have .NET Core project with the following project.json:
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
},
"scripts": {
"postcompile": [
"dotnet pack --no-build --configuration %compile:Configuration%"
]
}
}
I have created nuget package (see postcompile above) and published in nuget.
Then I've created standard 4.6.2 library, but cannot install package - there is error that package doesn't contain valid assemblies for target .NET Framework 4.6.2.
How should I prepare nuget package to make it available in standard library? I thiough that target NETStandard is valid for both - core and standard projects.
If you check the compatibility matrix for .Net Standard it looks like .Net 4.6.2 only supports NetStandard 1.5. Under frameworks you are targeting 1.6 that is marked for vNext.
See here for the compatibility matrix to know what version you should target
So in other words if you change your config to the following you should be able to reference the library freely:
"frameworks":
{
"netstandard1.5": {}
}
You don't need to add another framework type like suggested in another answer since 4.6.2 should be able to reference a 1.5 Standard.
you should be able to add support for .Net Framework 4.6.2 with adding "net462" to your project.json:
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
},
"net462": {
"dependencies" : {
"System.Runtime": "4.0.20.0" // you may have to add this
// add here your dependencies
}
}
},
"scripts": {
"postcompile": [
"dotnet pack --no-build --configuration %compile:Configuration%"
]
}
}
see also my project.json on github with a few supported frameworks

Referencing .NetCore Class Library in .NetCore Web Application

I have created a .NetCore Class Library which I have tested in same solution with .NetCore Console Application (and it works just fine), however when I try referencing it inside another (ASP.NET Core Web Application) solution as a NuGet package I always get a error
NU1002 The dependency YoutubeExtractorCore 0.0.6 does not support framework .NETCoreApp,Version=v1.0.
I have added the netcoreapp1.0 in the class library project.json, which looks like this:
{
"title": "YoutubeExtractorCore",
"description": ".NET Core library used for extracting information about Youtube videos",
"version": "0.0.6-*",
"dependencies": {
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50",
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
}
},
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
I spent this afternoon trying to fix the problem but I cannot even understand why this error occurs.
I finally figured out what was causing the problem. The NuGet package I created was packed with nuget.exe manually, and for some reason did not have the frameworks and their dependencies listed. I fixed the package by executing dotnet pack on the class library and this produced correcly packed NuGet package.

.NET versions in ASP.Core

I have a asp core web app with
"frameworks": {
"net452": {}
},
And I have also Class Library (.NET Core).
{
"version": "1.0.0-*",
"frameworks": {
"netstandard1.5": {
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"net452": {}
}
}
}
The question is, how can I add web app project reference to class library? Should I import some .net framework or use .net standard ?
Now I have a error message when I was trying to add reference to class library project.
The following projects are not supported as references:
"webprojectname" has target frameworks that are incompatible with
targets in current project "classlibraryprojectname"
"classlibraryprojectname": .NETCoreApp, Version=v1.0 "webprojectname":
.NETFramework, Version=v4.5.2
EDIT: Updated in accordance with changes to the question.
So I don't keep filling the comments section, your project.json's should look like this:
Web App (you will also have other dependencies not shown here):
"dependencies": {
"YourClassLibrary": "1.0.0-*"
},
"frameworks": {
"net452": { }
}
Class Library:
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.2": { }
}
}
Note: if your class library depends on something higher than netstandard1.2, then you will either have to increment the .Net Framework in your web app to match the NETSTandard you're using (as per previously linked docs), or you will need to cross-compile the class library like so:
{
"version": "1.0.0-*",
"frameworks": {
"netstandard1.5": {
"dependencies": {
"NETStandard.Library": "1.6.0"
}
},
"net452": { }
}
}
Using the first suggestion, your application produces one assembly for your class library, using this second method your class library will be compiled twice (one for netstandard1.5, one for net452). When your web app tries to resolve the class library, it realises it can't use netstandard1.5 (which targets net462), so falls back to the net452 assembly. Note however that using this second option is usually indicative of something not being right with your project structure, and should only be used if you know what you're doing and why (in my opinion).
The NETStandard.Library dependency shown is essential a NuGet bundle that pulls down the necessary CoreFX packages. If you get errors like "Cannot find Microsoft.CSharp" it's usually because you haven't pulled down the default packages. You can instead pull down only the CoreFX packages you want, such as System.Collections but this is a more advanced technique.
Hopefully that helps clear things up?

Add .NETFramework 4.5 dll to .NETCore project

I have a previously generated DLL with some enterprise code that I would like to reuse in a .NETCore project. However, since the original DLL was created and compiled using .NETFramework 4.5 I am not able to directly add the dll as a reference.
I created a nuget package containing my dll as shown here. After that, I am able to add such package to the project. However, I am having the following error:
"The dependency MyAssembly.dll does not support framework
.NETCoreApp,Version=v1.0"
I have already tried referencing .NETCore.Portable.Compatibility but I dont think that is the right way to go. How can I use this dll on my code?
This is what my project.json looks like after adding the Nuget package.
{
"version": "1.0.0-*",
"dependencies": {
"MyAssembly.dll": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [ "dnxcore50", "portable-net451+win8" ]
}
}
}
You can add more than one frameworks in project json file in your .netcore class library. By default you will have .net core framework. Just add comma and add your required .net framework.
{
"version": "1.0.0-*",
"frameworks": {
"netstandard1.6": {
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"imports": "dnxcore50"
},
"net45": {
"dependencies": {},
"imports": "net452"
}
}
}
If your .NET 4.5 dll is not compiled as PCL, you cannot reference it from project that targets netcoreapp1.0. It should be re-compiled as portable library or target netstandard API set.

Issue referencing a .Net Core Class project from a .Net Core Console Application

I am quite new to .Net Core and I am having an issue when trying to reference a .Net Core Class Assembly Project from my .Net Core Console Application.
It is a very similar issue to this question, however the answers here did not help me.
I am using Visual Studio 2015 Update 3
I am using ReSharper Ultimate 2016.1.2
I am using .Net Core 1.0.0 Preview 2 (RC2)
.Net Core Class project
Class1.cs
namespace ClassLibrary1
{
public class Class1
{
public void Foo()
{
Console.WriteLine("Foo");
}
}
}
project.json
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
.Net Core Console project
Program.cs
using System;
using ClassLibrary1;
namespace ConsoleApplication
{
public class Program
{
Class1.Foo();
}
}
project.json
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"ClassLibrary1": "1.0.0-*"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
I can successfully add a project reference to ClassLibrary1 in ConsoleApplication, this adds the reference and results in the dependency being added to my project.json in ConsoleApplication however when I try to use the reference eg. ClassLibrary1.Class1.Foo(); Visual Studio fails to find the code, fails to build.
ReSharper highlights my code as Red like the question above, and does offer to add the reference, however this does nothing.
I have tried to answers in the linked question, which didn't work for me, and I'm not sure it is ReSharper with the issue as Visual Studio just won't compile the code, which doesn't have a bearing on ReSharper.
If I change the dependency and framework of my Class1 in the project.json from netstandard1.6 to netcoreapp as below, this does work and allows me to reference and use the class as expected in my ConsoleApplication. However, this seems rather hacky to me and my understanding is that netcoreapp is a dependency used for a runnable assembly, and netstandard should be the dependency for a regular class library?
Does anyone have any thoughts?
Hacked ClassLibrary1/project.json (Does work but seems wrong?)
{
"version": "1.0.0-*",
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}

Categories

Resources