Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reference .net framework 4.7.2 class library from an ASP.NET Core MVC project?

I have an ASP.NET MVC 5 project which works well and it also references a .NET Framework 4.7.2 Class library which produces some CrystalReports. CrystalReports does not support .NET Core, so the class library will stay with full .NET Framework.

Now, if I upgrade the ASP.NET MVC 5 to ASP.NET Core 2 (or 3), will I be able to reference the class library and possibly generate those CrystalReports?

like image 787
John Avatar asked Nov 01 '19 11:11

John


People also ask

Can I use .NET Framework library in .NET Core project?

The answer is no, we cannot use . NET Framework Base Class Library in . NET Core because of compatibility issues. Basically, the libraries which target .

Can you reference .NET Framework from .NET Core?

NET Core, you need to use the . NET Framework. You can now reference . NET Framework libraries from .

Can ASP.NET Core work with the .NET Framework?

In order to run ASP.NET Core Apps on the . NET Framework it needs to only reference . Core NuGet packages which contains only the . NET Standard 2.0 builds.

How do I add a reference to class library in .NET Core?

Add Class Library Reference To use a class library in your application, you must add a reference to the library to access its functionality. Right click on the project name of your console app in Solution Explorer and select Add ->Reference option.


1 Answers

.NET Core doesn't support inclusion of .NET Framework libraries. Period. However, .NET Core supports .NET Standard, and since .NET Framework also implements .NET Standard, Microsoft made a special exception in the compiler to allow you include .NET Framework libraries, with the caveat that they may not actually function at all or totally. You get a warning to this effect when you include a .NET Framework library in a .NET Core project, and it's on you to ensure that the library works correctly end-to-end.

The largest majority of .NET Framework libraries do work, as long as they don't utilize .NET Framework-specific APIs (most notably Windows-specific APIs). If they do, then they will not work.

Here, it seems this library does utilize Windows-specific APIs, meaning it is incompatible with .NET Core. In such a situation, you can still create an ASP.NET Core project, but you must target .NET Framework, not .NET Core. That is, until ASP.NET Core 3.0, which cannot target .NET Framework. ASP.NET Core 3.0+ depends on .NET Standard 2.1, which no version of .NET Framework supports or ever will.

As such, if you need to use a .NET Framework library that is not 100% .NET Standard 2.0 compliant, you must target .NET Framework, and if you must target .NET Framework, you are then version-locked at 2.2 of ASP.NET Core.

UPDATE

This answer is a little old now, but just in case it might still be helpful, the way I've dealt with this kind of thing personally is to create a very small and simple API. Basically, you create a ASP.NET Core 2.1 (LTS) app targeting .NET Framework, and all this app does is interact with the .NET Framework library. Here that would mean creating the report. Then, you're free to create your actual app as an ASP.NET Core 3.1+ app targeting .NET Core, and you just call out to the API to get the data, report, whatever you need. It's sort of like a lightweight microservice approach.

like image 103
Chris Pratt Avatar answered Nov 15 '22 07:11

Chris Pratt