简体   繁体   中英

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?

.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.

Chris has already provided an excellent and accurate answer but I'll try to add a bit more color by sharing the results of an experiment I did.

Experiment: I have a web application targerting.Net Core 3.1. It calls a library that targets the Full Framework. In that library I specifically make a call to a Full Framework API that is not available in.Net Core 3.1, in this case that type is SHA512Cng .

My library code is:

        /// <summary>
        /// Returns a base64 encoded Sha512 hash of the text specified.
        /// Uses the SHA512Cng implementation to do the hashing.
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string Sha512Hash(string text) {

            using (SHA512Cng sha512Cng = new SHA512Cng()) {
                byte[] bytes = Encoding.ASCII.GetBytes(text);
                byte[] hashButes = sha512Cng.ComputeHash(bytes);
                return Convert.ToBase64String(hashButes);
            }
        }

In the home controller of my web application I make a call to use that Library method like so:

       public IActionResult Index() {
            string hash = App.Lib.Sha512Hash("hello world");

            return View();
        }

Pretty simple experiment. This code is running on a windows machine that has the full framework installed. If I call this library from a website targeting the Full Framework it works perfectly.

When I call this method in the Library from a.Net Core 3.1 website, what happens? That's the questions I wanted this experiment to answer.



It throws the following exception:

System.TypeLoadException: 'Could not load type 'System.Security.Cryptography.SHA512Cng' from assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.'

Screenshot:

在此处输入图像描述

It really doesn't matter whether your code is running on a box with the Full Framework or not.您的代码是否在具有完整框架的机器上运行并不重要。 If you reference a Full Framework Library from a website targeting Asp.Net Core 3 and make a call into a method that references a type that is incompatible with Asp.Net Core 3, then it's gonna throw .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM