Is there any way to use .NET Core Library from .NET Console App in same solution.
Example:
1) Create .NET Core Library project, add some class to test. Set frameworks to
"frameworks": {
"net45": { },
"netstandard1.5": { "imports": "dnxcore50" }
}
2) Create .NET45 console app in same project.
3) Try to reference library from console app. It's ok.
4) Try to use class from library in console app. You will get an error that class can't be found.
You can try add library namespace manually, but it will not help (same error for namespaces).
Workaround:
5) Remove reference to library project
6) Add reference to compiled library DLL for .NET45
7) All works!
Look like bug?
Yes, this experience is not rounded out yet in the tooling. As xproj gets aligned with csproj in future previews, this experience will start working.
The functionality to reference .NET Core projects from the full framework projects was the most important thing I was waiting for in RC2, ever since it was announced at one of asp.net community stand-ups. If you consider a scenario of porting parts of a large solution to .NET Core, this is definitely a blocker: you can't really port ~100 projects simultaneously, and you can't do it gradually if the tooling doesn't allow referencing new projects from classic projects (or vice versa).
I must admit, what I'm currently seeing in the RC2 tooling is kind of disappointing =( So... is there a new estimation on when we can expect .NET Framework -> .NET Core project references to start working properly?
+1 This is very important. I currently manually reference the produced .dll file in my COM add-in projects and others which cannot and will not be ported to .NET Core. This is a key scenario for me.
BTW, you will get the same behavior by creating a Net Core Console application.
1) Create a net core console application project.
2) Create a net core library project.
3) Reference the core library project in your project.json file at the root of the net core console app.
4) Now try referencing the library project with a simple using statement. Not happening; can't reference it and the intellisense doesn't even see it.
This is also important for those who want to write components like Powershell commands that can't be compiled as Net Core assemblies but need to reference Net Core assemblies to re-use business logic.
Agree with @HellBrick , I'm waiting for this functionality every day.
it still doesn't work in RTM, and really disappoint me.
+1 This was the very first thing I tried to do in .NET Core and it doesn't work. This might (scare people away (but hopefully not) if they think it can't be done. It may at least slow down adoption of .NET Core.
I kinda think that it a must to have feature, i just want to do a simple project reference and expecting it should be available from my ASP.NET Core project
This is getting ridiculous. I have linked code files all over my solution because I can't reference my own projects from my Powershell or MS Unit Test projects if I also want to reference them from my ASP.NET Core project. Which of course I do want to do, this is pretty standard and I've always done it in the past, obviously. Feels like we're taking huge steps backwards here.
Here is an answer on Stack Overflow from @onovotny on How to reference a .NET Core class library from a .NET 4.6 project.
Basically, you can create a Portable Class Library, which will be a .csproj, and retarget it to .NET Standard. You will be able to reference it from both .xproj and .csproj projects. Alternatively, if you want to use .xproj for your .NET Core class library, you can add a reference from the .csproj to the DLL in the output folder of the .xproj, and add a project build dependency in the solution. See the answer on Stack Overflow for more details.
Thanks for posting this link. I just tried it, and as far as I can tell (maybe I'm doing something wrong), it doesn't solve the problem. Or at least it doesn't solve my problem.
While it DOES let you reference the project from both xproj and csproj projects, those projects still have to be based on .NET Core or else the reference fails (for obvious reasons) with a "current project's target is not compatible with the targets of the referenced library" error.
So my Powershell and MS Unit Test projects still can't reference my .NET Core assemblies, because they have a different target (and they have to, because the assemblies they depend on haven't been ported to .NET Core yet).
This is a big problem. Here's why: if you have a new ASP.NET project, you cannot reuse any functionality or code that you write for it with any non-.NET Core library, because the latest version of ASP.NET requires .NET Core assemblies, and other projects (i.e. Powershell or MS Unit Test projects) can't reference .NET Core projects.
So while I can now publish my ASP.NET project to web servers on any platform (which I have NEVER needed to do and probably never will), I can't re-use my own code between my ASP.NET project and other projects, even for the purposes of unit testing. That's a huge problem.
@swaletzko Probably what is happening is that the .NET Core library is targeting a higher version of .NET Standard than works with the version of .NET Framework you are targeting. Target a lower version of .NET Standard with the .NET Core library and your .NET Framework projects should be able to reference it.
See the ".NET Platforms Support" table in the .NET Standard Library documentation to determine what versions of .NET Standard can be used with what version of .NET Framework. The table might be confusing, but basically if you are targeting .NET 4.5 then you can reference libraries targeting .NET Standard 1.1 or lower. If you target .NET 4.6 then you can reference .NET Standard 1.3 or lower.
I was about to respond to tell you that I had already double-checked the versions when I tested this (I did), and so that couldn't possibly be the problem.
Thankfully, before I did that I went ahead and triple-checked - and you are absolutely correct. Somehow I had mixed up the target version numbers of my projects in my head and the referencing library WAS older than the .NET Core library I was trying to reference, even though I thought it was the same.
So to sum up: creating a Portable library (in VS 2015 RC Update 3) and retargeting it to .Net Standard did the trick. I can now reference .Net Core projects from regular .Net Framework projects.
I take back my snarkiness from earler. Thank you @dsplaisted for having the patience to give me a second nudge in the right direction.
Sorry, I need a little more help here. After much wrangling I finally got all of my projects upgraded to the proper versions of whatever frameworks they use and they can all reference each other and all is well.
Except for my ASP.NET project, which is using framework "dnx461", and when I reference my other projects that use the "netstandard1.4" framework I get a "Project does not support framework DNX, Version=v4.6.1". But according to the documentation, .NET Standard 1.4 and .NET Framework 4.6.1 are compatible. Unless I'm reading that chart wrong, or doing something else wrong?
@swaletzko You shouldn't be using dnx461 anymore, use net461 instead.
Thanks, @svick but using "net461" has the same result: "[my referenced netstandard1.4 project] does not support framework DNX, Version=v4.6.1".
Edit: Seems to all be good now. Something was being cached, I deleted by "bin" and "packages" folders and restore / rebuilt and all references seem to be on the same page with each other. Thanks all for the help.
@swaletzko with that, I will close this issue since it seems the problem has been fixed.
I don't think the problem has been fixed since I can't reference xproj in csproj directly.
And it seems the PCL doesn't support netstandard1.6. If you retargeting your PCL project to netstandard1.6, you will get this message:Predefined type 'System.Object' is not defined or imported
I think this bug is NOT fixed yet. You can easily reproduce it:
1) Create a classic console app using full .net framework;
2) Create a net core library project, set framework to "net46";
3) Add reference from console project to library project;
4) Try to reference any type of the library project in the console app, you will get error saying the type does not exist;
See also: http://forums.asp.net/t/2098587.aspx?Reference+to+NET+Core+Class+Library+project+not+working
As far as the xproj references in csproj goes, that problem will be mitigated by the tooling's move to csproj all-up as was explained in the "Changes to project.json" blog post.
@swaletzko - this was a problem for me because the csproj file had ToolsVersion=12.0 instead of 14.0.
When referencing .net core dll from non-core projects, new projects work but projects upgraded from vs 2013 don't unless you change this value.
I don't think this should be closed because I just reproduced this as @yanglee said, and my Tools Version is 14.0 on all involved projects
Please do not close this issue.
Might help in fixing: building the core project writes dll files to the netcoreapp1.0 folder but not to the other target folder(s). If I try to add a reference by picking the dll in that folder it works (from a net461 project).
Is this functionality improved at all in .NET Core 2.x?
@stamminator Referencing a .NET Core library from a .NET Framework project is not supported (intentionally). If you want to create a library that can be used from both .NET Core and .NET Framework, then you should target .NET Standard (probably .NET Standard 2.0).
Thanks!
Most helpful comment
I think this bug is NOT fixed yet. You can easily reproduce it:
1) Create a classic console app using full .net framework;
2) Create a net core library project, set framework to "net46";
3) Add reference from console project to library project;
4) Try to reference any type of the library project in the console app, you will get error saying the type does not exist;
See also: http://forums.asp.net/t/2098587.aspx?Reference+to+NET+Core+Class+Library+project+not+working