简体   繁体   English

.NET Standard与.NET Core

[英].NET Standard vs .NET Core

I have read about the difference between .NET Standard and .NET Core, but I really don't know what the difference is, or when to choose a .NET Standard library project and when to choose a .NET Core library project. 我已经了解了.NET Standard和.NET Core之间的区别,但我真的不知道区别是什么,或者何时选择.NET标准库项目以及何时选择.NET Core库项目。

I have read that .NET Standard is to ensure that a set of APIs are always available, no matter the platform used (as long as that platform is compatible with the .NET Standard version that I have chosen). 我已经读过.NET Standard是为了确保一组API始终可用,无论使用哪个平台(只要该平台与我选择的.NET标准版本兼容)。 If I'm not mistaken, this means that I can create a class library of .NET Standard and then use it on any platform that is compatible with the .NET Standard version that I have chosen. 如果我没有弄错的话,这意味着我可以创建一个.NET Standard类库,然后在任何与我选择的.NET Standard版本兼容的平台上使用它。

With .NET Core, I have read that it is intended for cross-platform use too, so if I choose a .NET Core library it seems that I can use it on many platforms too, just like .NET Standard. 使用.NET Core,我已经读过它也是用于跨平台的,所以如果我选择一个.NET Core库,我似乎也可以在许多平台上使用它,就像.NET Standard一样。

So at the end, I don't see the difference. 所以最后,我没有看到差异。 When should I use which? 我什么时候应该使用哪个? What is the difference between them? 他们之间有什么区别?

I will try to further clarify your doubts and extend Jon Skeet answer. 我将尝试进一步澄清您的疑虑,并延长Jon Skeet的答案。

.NET Standard is a specification , so a library compiled for a specific .NET Standard version can be used in different .NET Standard implementations. .NET Standard是一个规范 ,因此为特定.NET Standard版本编译的库可以在不同的.NET Standard实现中使用。

As said in my other comment, a good analogy for the relationship between .NET Standard and other .NET Standard Implementations (.NET Core, .NET Framework, etc) is this gist by David Fowler : .NET Standard versions are Interfaces , while frameworks are implementations of those interfaces. 正如我在其他评论中所说,.NET标准与其他.NET标准实现(.NET Core,.NET Framework等)之间关系的一个很好的类比是David Fowler的这个要点 :.NET标准版本是Interfaces ,而框架是那些接口的实现。

This simplified diagram may help to understand this relationship: 这个简化的图表可能有助于理解这种关系:

NET标准接口类比

Anything targetting NetCore10 has access to INetStandard15 APIs and NetCore10 specific APIs (such as DotNetHostPolicy ). 任何针对NetCore10东西都可以访问INetStandard15 API NetCore10 特定的 API(例如DotNetHostPolicy )。

Of course this library cannot be used in different INetStandard15 implementations ( NetCore10 is not convertible to NetFramework462 or Mono46 ). 当然,这个库不能用于不同的INetStandard15实现( NetCore10不能转换为NetFramework462Mono46 )。

If you, instead, need access only to INetStandard15 APIs (and target that specification instead of a concrete framework) your library may be used by any framework which implements it ( NetCore10 , NetFramework462 , etc.) 相反,如果您只需要访问INetStandard15 API(并且针对该规范而不是具体框架),那么任何实现它的框架NetCore10NetFramework462等)都可以使用您的库。

Note: in the original analogy David Fowler used interfaces for both .NET Standard versions and frameworks implementations. 注意:在最初的类比中,David Fowler使用.NET标准版本和框架实现的接口。 I believe that using interfaces and classes is, instead, more intuitive and better represents the relationship between specifications and concrete implementations. 我相信使用接口和类更直观,更好地代表了规范和具体实现之间的关系。

.NET Core is an implementation of .NET Standard. .NET Core是.NET Standard的一种实现 It's available on multiple operating systems, but that's not the same thing - there are other implementations of .NET Standard as well. 它可以在多个操作系统上使用,但这不是一回事 - 还有.NET Standard的其他实现。

So if you create a .NET Core library, it will have access to things that are implemented in .NET Core, but aren't part of .NET Standard, and your library won't be compatible with other implementations of .NET Standard, such as Xamarin, Tizen, full .NET desktop framework etc. 因此,如果您创建.NET Core库,它将可以访问在.NET Core中实现但属于.NET Standard的内容,并且您的库将与.NET Standard的其他实现不兼容,如Xamarin,Tizen,完整的.NET桌面框架等。

In short: to achieve maximum portability, make your library target .NET Standard. 简而言之:要实现最大的可移植性,请使您的库以.NET Standard为目标。

.NET Core Class library is basically subset of .NET Framework library, which just contains less APIs. .NET核心类库基本上是.NET Framework库的子集,它只包含较少的API。 Sticking to .NET Core Class library makes difficult to share code between runtimes. 坚持使用.NET Core Class库很难在运行时之间共享代码。 This code might not work for different runtime (Mono for Xamarin), because it doesn't have the API that you need. 此代码可能不适用于不同的运行时(Mono for Xamarin),因为它没有您需要的API。 To solve this there is .NET Standard, which is just set of specification that tells you which APIs you can use . 为了解决这个问题,有.NET标准,它只是一套规范,告诉你可以使用哪些API Main purpose of .NET Standard is to share code between runtimes. .NET Standard的主要目的是在运行时之间共享代码。 And important that this specification implemented by all runtimes.(.NET Framework, .NET Core and Mono for Xamarin). 并且重要的是,此规范由所有运行时实现。(。NET Framework, .NET Core和Mono for Xamarin)。

So if you sure that you will use your library only for .NET Core projects, you can ignore .NET Standard, but if have even tiny chance that your code will be used by .NET Framework or Mono for Xamarin then better stick to .NET Standard 因此,如果您确定仅将库用于.NET Core项目,则可以忽略.NET Standard,但如果.NET Framework或Mono for Xamarin使用您的代码的可能性很小,那么最好坚持使用.NET。标准

Also note that higher version of .NET Standard contain more APIs, but lower version supported by more platforms. 另请注意,更高版本的.NET Standard包含更多API,但更多平台支持更低版本。 Therefore if you create .NET Standard library that you want to share between runtimes then target the lowest version you can , which help you reach maximum amounts of platforms. 因此,如果您创建要在运行时之间共享的.NET标准库,则可以使用最低版本 ,这可以帮助您达到最大数量的平台。 For example, if you want to run on .NET Framework 4.5 and .NET Core 1.0, the highest .NET Standard version you can use is .NET Standard 1.1. 例如,如果要在.NET Framework 4.5和.NET Core 1.0上运行,则可以使用的最高.NET标准版本是.NET Standard 1.1。 This this great table from documentation for more info about it. 这个伟大的表格来自文档 ,有关它的更多信息。

PS: Also if you want to convert you library to .NET Standard, .NET Portability Analyzer could help you with that. PS:此外,如果您想将库转换为.NET Standard,.NET Portability Analyzer可以帮助您。

.NET Standard is a specification of APIs that all .NET implementations must provide. .NET Standard是所有.NET实现必须提供的API规范。 It brings consistency to the .NET family and enables you to build libraries you can use from any .NET implementation. 它为.NET系列带来了一致性,使您能够构建可以从任何.NET实现中使用的库。 It replaces PCLs for building shared components. 它取代了用于构建共享组件的PCL。

.NET Core is an implementation of the .NET Standard that's optimized for building console applications, Web apps and cloud services using ASP.NET Core. .NET Core是.NET标准的一种实现,它针对使用ASP.NET Core构建控制台应用程序,Web应用程序和云服务进行了优化。 Its SDK comes with a powerful tooling that in addition to Visual Studio development supports a full command line-based development workflow. 它的SDK附带了一个功能强大的工具,除了Visual Studio开发支持完整的基于命令行的开发工作流程。 You can learn more about them at aka.ms/netstandardfaq and aka.ms/netcore . 您可以在aka.ms/netstandardfaqaka.ms/netcore上了解有关它们的更多信息。


The above, together with a very clear explanation of most of the stuff discussed in this question can be found in the following extremely helpful article by Microsoft (MSDN - September 2017): .NET Standard - Demystifying .NET Core and .NET Standard 以上,以及对此问题中讨论的大多数内容的非常清楚的解释,可以在Microsoft的以下非常有用的文章中找到(MSDN - 2017年9月) :.NET标准 - 揭开.NET核心和.NET标准的神秘面纱

In simple terms, .NET standard is used for writing class library projects which compiles to dll. 简单来说,.NET标准用于编写编译为dll的类库项目。 .NET Core can be used for developing actual web applications which can run on all operating systems (Windows, Linux, MacOS). .NET Core可用于开发可在所有操作系统(Windows,Linux,MacOS)上运行的实际Web应用程序。 (In .NET Core 3 Microsoft has provide the functionality to develop desktop apps using WPF, but uptil now these apps will not be cross platform and will only run on windows system. In future Microsoft might make them cross-platform too) .NET standard libraries/dlls can be used in any application which uses .NET (.NET framework, .NET Core) which means that you can use .NET standard with both .NET framework and .NET core. (在.NET Core 3中,Microsoft提供了使用WPF开发桌面应用程序的功能,但现在这些应用程序不会跨平台,只能在Windows系统上运行。未来Microsoft也可能使它们跨平台).NET标准libraries / dll可以在任何使用.NET(.NET框架,.NET Core)的应用程序中使用,这意味着您可以将.NET标准与.NET框架和.NET核心一起使用。

.NET Standard is a specification of .NET APIs intended to be available on .NET implementations. .NET Standard是.NET API的规范,旨在.NET实现上可用。 This enables to define uniform set of BCL APIs for all .NET implementations. 这使得能够为所有.NET实现定义统一的BCL API集。

.NET Core is one such implementation of .NET Standard. .NET Core就是.NET Standard的一种实现。 .NET Framework is another implementation of .NET Standard. .NET Framework是.NET Standard的另一种实现。

Image from .NET Blog 来自.NET博客的图片

在此输入图像描述

Federicos answer gives you a graphical overview of how each framework evolve with versions. Federicos的答案为您提供了每个框架如何随版本发展的图形概述。 Take a look at below diagram from Microsoft Docs . 请查看Microsoft Docs的下图。

在此输入图像描述

Targeting .NET Standard increases your platform support whereas targeting a particular .NET platform such as .NET Core (or .NET Framework) will allow you to use all the platform features for that platform. 定位 .NET Standard可增加您的平台支持,而针对特定.NET平台(如.NET Core(或.NET Framework))将允许您使用该平台的所有平台功能。

Did you mean .NET Framework? 你的意思是.NET Framework吗? Because .NET standard is an implementations, such as .NET Framework, .NET Core and Xamarin. 因为.NET标准是一种实现,例如.NET Framework,.NET Core和Xamarin。

I love .NET Core because we can host it on Linux (use nginx in my experience). 我喜欢.NET Core,因为我们可以在Linux上托管它(根据我的经验使用nginx)。 It's different than .NET framework which is you can only host on IIS. 它与.NET框架不同,它只能在IIS上托管。 You can consider about hosting budget in this case (Because windows server is expensive for me). 在这种情况下你可以考虑托管预算(因为Windows服务器对我来说很昂贵)。

In the development environment perspective , .Net core is lightweight. 开发环境的角度来看 ,.Net核心是轻量级的。 So, you can use VSCode, Sublime, for IDE (not only visual studio). 因此,你可以使用VSCode,Sublime,用于IDE(不仅仅是visual studio)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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