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

后端 未结 2 2109
刺人心
刺人心 2020-12-30 16:10

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 supp

相关标签:
2条回答
  • 2020-12-30 16:41

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

    0 讨论(0)
  • 2020-12-30 17:01

    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.

    And the answer is...

    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:

    So the takeaway is this: 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.

    0 讨论(0)
提交回复
热议问题