What are the problems with mixing .NET Framework and .NET CORE? [closed]

烈酒焚心 提交于 2019-12-14 03:34:54

问题


I have a load of middle and back end components (repositories, services etc) that were written against the .NET Framework 4 but that are still relevant to a new project that I'm now working on. The front end of this new project will be written in ASP .NET CORE2.2. What should I do - recreate all components in .NET CORE or keep the back end running against .NET Framework while the front end runs against CORE? What are the considerations in terms of e.g. performance?


回答1:


By components, I'd imagine you're talking about class libraries. What you should realize is that the target framework of a class library basically just specifies a particular API compatibility. The actual framework code that's packaged in the end is based on the actual app's target framework.

In other words, you can add a reference to a class library project that targets .NET Framework to an app that targets .NET Core and then publish that app to a computer that only has .NET Core installed, and it will run (without .NET Framework). Theoretically, at least.

.NET Core doesn't technically support .NET Framework references. It supports .NET Standard references. However, the API footprint of .NET Standard 2.0 was large enough, and the back catalogue of .NET Framework NuGet packages and other libraries floating around the Internet was large enough, that Microsoft tweaked the compiler to allow direct referencing of .NET Framework libraries. The majority of .NET Framework-targeting code could actually target .NET Standard instead, without change, and Microsoft determined it was safe enough to roll the dice in most cases, rather than waiting on every library maintainer to change the target framework of their libraries to .NET Standard (many of these libraries aren't even maintained any more).

However, there's a big caveat. When you do this, you'll get a compiler warning, basically telling you that there's no guarantees here. It's going to let you add the reference and try to use it, but if there's any APIs utilized that aren't supported by .NET Core, it'll crash and burn. It's then up to you to thoroughly test your app, ensure that nothing is broken, and then you can suppress the warning, if you like.

Long and short, you can directly reference .NET Framework libraries in .NET Core 2.0+. They may not actually end up working, but as long as they don't use any unsupported APIs (usually Windows-specific stuff such as drawing, audio, GUI/paint, etc.), then you'll be fine.

However, if you maintain these libraries, your best bet is to retarget them to .NET Standard 2.0. If you can get them to compile on that, then you'll know there won't be any issues, instead of just guessing or hoping, and it won't have any impact on any existing .NET Framework applications that use them as well.



来源:https://stackoverflow.com/questions/57205650/what-are-the-problems-with-mixing-net-framework-and-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!