是否可以从ASP.NET Core MVC项目中引用.net Framework 4.7.2类库? [英] Is it possible to reference .net framework 4.7.2 class library from an ASP.NET Core MVC project?

查看:449
本文介绍了是否可以从ASP.NET Core MVC项目中引用.net Framework 4.7.2类库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行良好的ASP.NET MVC 5项目,它还引用了一个.NET Framework 4.7.2类库,该类库生成了一些CrystalReports. CrystalReports不支持.NET Core,因此该类库将与完整的.NET Framework一起使用.

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.

现在,如果我将ASP.NET MVC 5升级到ASP.NET Core 2(或3),是否可以引用类库并可能生成那些CrystalReports?

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不支持包含.NET Framework库.时期.但是,.NET Core支持.NET Standard,并且由于.NET Framework也实现.NET Standard,因此Microsoft在编译器中做了一个特殊的例外,允许您包括.NET Framework库,但要注意,它们可能实际上根本不起作用或完全.当您在.NET Core项目中包含.NET Framework库时,您会收到有关此效果的警告,并且您必须确保该库端到端正确运行.

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

大多数.NET Framework库 都可以工作,只要它们不使用.NET Framework特定的API(最著名的是Windows特定的API)即可.如果这样做,那么它们将无法工作.

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.

在这里,该库确实使用Windows特定的API,这意味着它与.NET Core不兼容.在这种情况下,您仍然可以创建一个ASP.NET Core项目,但必须 目标是.NET Framework,而不是.NET Core.也就是说,直到不能以.NET Framework为目标的ASP.NET Core 3.0为止. ASP.NET Core 3.0+依赖于.NET Standard 2.1,没有任何版本的.NET Framework支持或永远不会.

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.

因此,如果您需要使用不符合100%.NET Standard 2.0的.NET Framework库,则必须以.NET Framework为目标,如果必须以.NET Framework为目标,则必须将版本锁定在2.2.ASP.NET Core.

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.

更新

这个答案现在有点老了,但是为了以防万一它仍然有用,我亲自处理这种事情的方法是创建一个非常小而简单的API.基本上,您创建了一个针对.NET Framework的ASP.NET Core 2.1(LTS)应用程序,而该应用程序所做的全部工作就是与.NET Framework库进行交互.这意味着创建报告.然后,您可以自由地将实际的应用程序创建为以.NET Core为目标的ASP.NET Core 3.1+应用程序,并且只需调用API即可获取数据,进行报告,无论您需要什么.有点像轻量级微服务方法.

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.

这篇关于是否可以从ASP.NET Core MVC项目中引用.net Framework 4.7.2类库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆