.NET Standard 与 .NET Core

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44085424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:55:47  来源:igfitidea点击:

.NET Standard vs .NET Core

.net.net-core.net-standard

提问by álvaro García

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 Standard 库项目以及什么时候选择 .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). 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 是为了确保一组 API 始终可用,无论使用什么平台(只要该平台与我选择的 .NET Standard 版本兼容)。如果我没记错的话,这意味着我可以创建 .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?

所以最后,我看不出有什么区别。我什么时候应该使用哪个?它们之间有什么区别?

采纳答案by Federico Dipuma

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.

正如我在其他评论中所说,David Fowler 的这个要点是 .NET Standard 和其他 .NET Standard 实现(.NET Core、.NET Framework 等)之间关系的一个很好的类比:.NET Standard 版本是Interfaces,而框架是这些接口的实现。

This simplified diagram may help to understand this relationship:

这张简化图可能有助于理解这种关系:

NET Standard Interfaces analogy

NET 标准接口类比

Anything targetting NetCore10has access to INetStandard15APIs andNetCore10specificAPIs (such as DotNetHostPolicy).

任何目标NetCore10都可以访问INetStandard15APINetCore10特定的API(例如DotNetHostPolicy)。

Of course this library cannot be used in different INetStandard15implementations (NetCore10is not convertible to NetFramework462or Mono46).

当然,这个库不能用于不同的INetStandard15实现(NetCore10不可转换为NetFramework462Mono46)。

If you, instead, need access only to INetStandard15APIs (and target that specification instead of a concrete framework) your library may be used by anyframework 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. I believe that using interfaces and classes is, instead, more intuitive and better represents the relationship between specifications and concrete implementations.

注意:在最初的类比中,David Fowler 使用了 .NET Standard 版本和框架实现的接口。我认为,相反,使用接口和类更直观,更好地表示规范和具体实现之间的关系。

回答by Jon Skeet

.NET Core is an implementationof .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 Core 是.NET Standard的实现。它可在多个操作系统上使用,但这不是一回事 - .NET Standard 也有其他实现。

So if you create a .NET Core library, it will have access to things that are implemented in .NET Core, but aren'tpart of .NET Standard, and your library won't be compatible with otherimplementations 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。

回答by user2771704

.NET Core Class library is basically a subset of .NET Framework library, which just contains fewer APIs. Sticking to .NET Core Class library makes it difficult to share code between runtimes. This code might not work for a different runtime (Mono for Xamarin), because it doesn't have the API that you need. To solve this there is .NET Standard, which is just set of specification that tells you which APIs you can use. The main purpose of .NET Standard is to share code between runtimes. And it's important that this specification is implemented by all runtimes. (.NET Framework, .NET Coreand Mono for Xamarin).

.NET Core 类库基本上是 .NET Framework 库的一个子集,它只包含较少的 API。坚持使用 .NET Core 类库使得在运行时之间共享代码变得困难。此代码可能不适用于不同的运行时(Xamarin 的 Mono),因为它没有您需要的 API。为了解决这个问题,有.NET Standard,它只是一组规范,告诉您可以使用哪些 API。.NET Standard 的主要目的是在运行时之间共享代码。并且重要的是该规范由所有运行时实现。(.NET Framework、.NET Core和 Mono for Xamarin)。

So if you are sure that you will use your library only for .NET Core projects, you can ignore .NET Standard, but if there is even a tiny chance that your code will be used by .NET Framework or Mono for Xamarin then it's better to stick to .NET Standard

因此,如果您确定将您的库仅用于 .NET Core 项目,则可以忽略 .NET Standard,但如果您的代码被 .NET Framework 或 Mono for Xamarin 使用的可能性甚至很小,那么最好坚持 .NET 标准

Also note that higher versions of .NET Standard contain more APIs, but lower versions are supported by more platforms. Therefore if you create a .NET Standard library that you want to share between runtimes then target the lowest version you can, which helps you reach the most platforms. 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. Refer to this great table from the documentationfor more info about it.

另请注意,更高版本的 .NET Standard 包含更多 API,但更多平台支持更低版本。因此,如果您创建了一个想要在运行时之间共享的 .NET Standard 库,那么请尽可能以最低版本为目标,这有助于您覆盖最多的平台。例如,如果要在 .NET Framework 4.5 和 .NET Core 1.0 上运行,则可以使用的最高 .NET Standard 版本是 .NET Standard 1.1。有关它的更多信息,请参阅文档中的这个很棒的表格

PS: Also if you want to convert your library to .NET Standard, the .NET Portability Analyzer could help you with that.

PS:此外,如果您想将库转换为 .NET Standard,.NET Portability Analyzer 可以帮助您。

回答by Nipuna

.NET Standardis a specification of .NET APIs intended to be available on .NET implementations. This enables to define uniform set of BCL APIs for all .NET implementations.

.NET Standard是 .NET API 的规范,旨在用于 .NET 实现。这允许为所有 .NET 实现定义统一的 BCL API 集。

.NET Coreis one such implementation of .NET Standard. .NET Framework is another implementation of .NET Standard.

.NET Core就是 .NET Standard 的一种这样的实现。.NET Framework 是 .NET Standard 的另一种实现。

Image from .NET Blog

图片来自.NET 博客

enter image description here

在此处输入图片说明

Federicos answergives you a graphical overview of how each framework evolve with versions. Take a look at below diagram from Microsoft Docs.

Federicos 的回答为您提供了每个框架如何随版本演变的图形概述。看看下面来自 Microsoft Docs 的图表。

enter image description here

在此处输入图片说明

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标准增加你的平台支持,而针对某一特定.NET平台,如.NET核心(或.NET框架)将允许您使用所有平台功能该平台。

回答by steliosalex

.NET Standardis a specification of APIs that all .NET implementations must provide. It brings consistency to the .NET family and enables you to build libraries you can use from any .NET implementation. It replaces PCLs for building shared components.

.NET Coreis an implementation of the .NET Standard that's optimized for building console applications, Web apps and cloud services using ASP.NET Core. Its SDK comes with a powerful tooling that in addition to Visual Studio development supports a full command line-based development workflow. You can learn more about them at aka.ms/netstandardfaqand aka.ms/netcore.

.NET Standard是所有 .NET 实现必须提供的 API 规范。它为 .NET 系列带来了一致性,并使您能够构建可以从任何 .NET 实现使用的库。它取代 PCL 来构建共​​享组件。

.NET Core是 .NET Standard 的一种实现,它针对使用 ASP.NET Core 构建控制台应用程序、Web 应用程序和云服务进行了优化。它的 SDK 带有一个强大的工具,除了 Visual Studio 开发之外,它还支持完整的基于命令行的开发工作流。您可以在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 Standard - Demystifying .NET Core and .NET Standard

回答by Fityan Aula

Did you mean .NET Framework? Because .NET standard is an implementations, such as .NET Framework, .NET Core and Xamarin.

你是说 .NET Framework 吗?因为 .NET 标准是一种实现,例如 .NET Framework、.NET Core 和 Xamarin。

I love .NET Core because we can host it on Linux (use nginx in my experience). It's different than .NET framework which is you can only host on IIS. You can consider about hosting budget in this case (Because windows server is expensive for me).

我喜欢 .NET Core,因为我们可以在 Linux 上托管它(根据我的经验使用 nginx)。它与 .NET 框架不同,后者只能托管在 IIS 上。在这种情况下,您可以考虑托管预算(因为 Windows 服务器对我来说很贵)。

In the development environment perspective, .Net core is lightweight. So, you can use VSCode, Sublime, for IDE (not only visual studio).

开发环境的角度来看,.Net core 是轻量级的。因此,您可以将 VSCode、Sublime 用于 IDE(不仅是 Visual Studio)。

回答by Waleed Naveed

In simple terms, .NET standard is used for writing class library projects which compiles to dll. .NET Core can be used for developing actual web applications which can run on all operating systems (Windows, Linux, MacOS). (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标准用于编写编译为dll的类库项目。.NET Core 可用于开发可在所有操作系统(Windows、Linux、MacOS)上运行的实际 Web 应用程序。(在 .NET Core 3 中微软提供了使用 WPF 开发桌面应用程序的功能,但到目前为止这些应用程序不会跨平台,只能在 Windows 系统上运行。未来微软可能也会使它们跨平台) .NET 标准库/dll 可用于任何使用 .NET(.NET 框架、.NET Core)的应用程序,这意味着您可以将 .NET 标准与 .NET 框架和 .NET 核心一起使用。