适用于服务器应用的 .NET Core 和. .NET Framework.NET Core vs. .NET Framework for server apps

有两种支持的 .NET 实现可用于生成服务器端应用:.NET Framework 和 .NET Core。There are two supported .NET implementations for building server-side apps: .NET Framework and .NET Core. 这两者共享许多相同的组件,可在它们之间共享代码。Both share many of the same components and you can share code across the two. 但两者之间存在根本的差异,可根据需要实现的目标进行选择。However, there are fundamental differences between the two and your choice depends on what you want to accomplish. 本文介绍了在何种情况下进行选择。This article provides guidance on when to use each.

在以下情况,对服务器应用程序使用 .NET Core:Use .NET Core for your server application when:

  • 用户有跨平台需求。You have cross-platform needs.
  • 你正在以微服务为目标。You're targeting microservices.
  • 你正在使用 Docker 容器。You're using Docker containers.
  • 需要高性能和可扩展的系统。You need high-performance and scalable systems.
  • 需按应用程序提供并行的 .NET 版本。You need side-by-side .NET versions per application.

在以下情况,对服务器应用程序使用 .NET Framework :Use .NET Framework for your server application when:

  • 应用当前使用 .NET Framework(建议扩展而不是迁移)。Your app currently uses .NET Framework (recommendation is to extend instead of migrating).
  • 应用使用不可用于 .NET Core 的第三方 .NET 库或 NuGet 包。Your app uses third-party .NET libraries or NuGet packages not available for .NET Core.
  • 应用使用不可用于 .NET Core 的 .NET 技术。Your app uses .NET technologies that aren't available for .NET Core.
  • 应用使用不支持 .NET Core 的平台。Your app uses a platform that doesn’t support .NET Core. Windows、macOS 和 Linux 支持 .NET Core。Windows, macOS, and Linux support .NET Core.

选择 .NET Core 的情形When to choose .NET Core

以下各部分对前面提到的选择 .NET Core 的原因进行了进一步说明。The following sections give a more detailed explanation of the previously stated reasons for picking .NET Core.

跨平台需求Cross-platform needs

如果应用程序(Web/服务)需要在多个平台(Windows、Linux 和 macOS)上运行,请使用 .NET Core。If your application (web/service) needs to run on multiple platforms (Windows, Linux, and macOS), use .NET Core.

.NET Core 作为开发工作站支持前面提到的操作系统。.NET Core supports the previously mentioned operating systems as your development workstation. Visual Studio 提供了适用于 Windows 和 macOS 的集成开发环境 (IDE)。Visual Studio provides an Integrated Development Environment (IDE) for Windows and macOS. 还可使用运行于 macOS、Linux 和 Windows 上的 Visual Studio Code。You can also use Visual Studio Code, which runs on macOS, Linux, and Windows. Visual Studio Code 支持 .NET Core,包括 IntelliSense 和调试。Visual Studio Code supports .NET Core, including IntelliSense and debugging. 大多数第三方编辑器(如 Sublime、Emacs 和 VI)都可搭配 .NET Core 使用。Most third-party editors, such as Sublime, Emacs, and VI, work with .NET Core. 这些第三方编辑器可使用 Omnisharp 获取编辑器 IntelliSense。These third-party editors get editor IntelliSense using Omnisharp. 也可不使用任何代码编辑器,直接使用适用于所有支持平台的 .NET Core CLIYou can also avoid any code editor and directly use the .NET Core CLI, available for all supported platforms.

微服务体系结构Microservices architecture

微服务体系结构允许跨服务边界组合使用技术。A microservices architecture allows a mix of technologies across a service boundary. 通过这种技术组合,可逐步接受 .NET Core 作为能与其他微服务或服务搭配使用的新微服务。This technology mix enables a gradual embrace of .NET Core for new microservices that work with other microservices or services. 例如,可组合使用微服务或使用 .NET Framework、Java、Ruby 或其他单片技术开发的服务。For example, you can mix microservices or services developed with .NET Framework, Java, Ruby, or other monolithic technologies.

可用的基础结构平台有很多。There are many infrastructure platforms available. Azure Service Fabric,设计用于大型和复杂微服务系统 。Azure Service Fabric is designed for large and complex microservice systems. Azure App Service,很适合用于无状态微服务。Azure App Service is a good choice for stateless microservices. 基于 Docker 的微服务备选方案适合任何一种微服务方法,这部分内容将在容器部分进行说明。Microservices alternatives based on Docker fit any kind of microservices approach, as explained in the Containers section. 所有这些平台都支持 .NET Core,是托管微服务的理想选择。All these platforms support .NET Core and make them ideal for hosting your microservices.

有关微服务体系结构的详细信息,请参阅 .NET 微服务 - 适用于容器化 .NET 应用程序的体系结构For more information about microservices architecture, see .NET Microservices. Architecture for Containerized .NET Applications.

容器Containers

容器通常与微服务体系结构结合使用。Containers are commonly used in conjunction with a microservices architecture. 还可使用容器将遵循任何体系结构模式的 Web 应用或服务容器化。Containers can also be used to containerize web apps or services that follow any architectural pattern. 可在 Windows 容器上使用 .NET Framework,但 .NET Core 的模块化和轻型性质使之成为容器的更佳选择。.NET Framework can be used on Windows containers, but the modularity and lightweight nature of .NET Core makes it a better choice for containers. 在创建和部署容器时,使用 .NET Core 时容器的映像大小要远小于使用 .NET Framework 时的大小。When creating and deploying a container, the size of its image is much smaller with .NET Core than with .NET Framework. 例如,因为它是跨平台的,所以可将服务器应用部署到 Linux Docker 容器。Because it's cross-platform, you can deploy server apps to Linux Docker containers, for example.

Docker 容器可托管在自己的 Linux 或 Windows 基础结构中,或托管在 Azure Kubernetes 服务等云服务中。Docker containers can be hosted in your own Linux or Windows infrastructure, or in a cloud service such as Azure Kubernetes Service. Azure Kubernetes 服务可管理、协调和缩放云中基于容器的应用程序。Azure Kubernetes Service can manage, orchestrate, and scale container-based applications in the cloud.

高性能和可扩展的系统High-performance and scalable systems

如果系统需要最佳的性能和可伸缩性,.NET Core 和 ASP.NET Core 是最佳的选择。When your system needs the best possible performance and scalability, .NET Core and ASP.NET Core are your best options. Windows Server 和 Linux 的高性能服务器运行时使 .NET 成为 TechEmpower 基准上执行最佳的 Web 框架。High-performance server runtime for Windows Server and Linux makes .NET a top performing web framework on TechEmpower benchmarks.

性能和可伸缩性对微服务体系结构尤为重要,体系结构中可能正在运行数百个微服务。Performance and scalability are especially relevant for microservices architectures, where hundreds of microservices may be running. 借助 ASP.NET Core,系统运行的服务器/虚拟机 (VM) 数要低得多。With ASP.NET Core, systems run with a much lower number of servers/Virtual Machines (VM). 减少服务器/VM 后可节省基础结构和托管成本。The reduced servers/VMs save costs in infrastructure and hosting.

按应用程序级别并行安装 .NET 版本Side by side .NET versions per application level

若要安装含不同 .NET 版本上的依赖项的应用程序,建议使用 NET Core。To install applications with dependencies on different versions of .NET, we recommend .NET Core. .NET Core 支持在同一计算机上并行安装不同版本的 .NET Core 运行时。.NET Core supports side-by-side installation of different versions of the .NET Core runtime on the same machine. 并行安装允许在同一服务器上使用多个服务,每个服务位于其相应的 .NET Core 版本上。This side-by-side installation allows multiple services on the same server, each of them on its own version of .NET Core. 这还可在应用程序升级和 IT 运营时降低风险、节省成本。It also lowers risks and saves money in application upgrades and IT operations.

.NET Framework 不支持并行安装。Side-by-side installation isn’t possible with .NET Framework. 它是一个 Windows 组件,一次只能有一个版本存在于计算机上。It’s a Windows component, and only one version can exist on a machine at a time. .NET Framework 的每个版本均替换之前的版本。Each version of .NET Framework replaces the previous version. 如果安装面向 .NET Framework 更高版本的新应用,则可能会中断计算机上运行的现有应用,因为替换了之前的版本。If you install a new app that targets a later version of .NET Framework, you might break existing apps that run on the machine, because the previous version was replaced.

选择 .NET Framework 的情形When to choose .NET Framework

.NET Core 对新应用程序和应用程序模式特别有用。.NET Core offers significant benefits for new applications and application patterns. 但是在很多现有方案中依然会自然而然地选择 .NET Framework,并且对于所有服务器应用程序,.NET Framework 不会被 .NET Core 代替。However, .NET Framework continues to be the natural choice for many existing scenarios, and as such, .NET Framework isn't replaced by .NET Core for all server applications.

现有的 .NET Framework 应用程序Current .NET Framework applications

在大多数情况下,不需要将现有应用程序迁移到 .NET Core。In most cases, you don’t need to migrate your existing applications to .NET Core. 相反,若要扩展现有的应用程序(例如,在 ASP.NET Core 中写入新的 Web 服务),建议使用 .NET Core。Instead, a recommended approach is to use .NET Core as you extend an existing application, such as writing a new web service in ASP.NET Core.

.NET Framework 上的 ASP.NET CoreASP.NET Core on .NET Framework

有关 .NET Framework 上的 ASP.NET Core 支持的信息,请参阅 .NET Core 支持策略For information about support for ASP.NET Core on .NET Framework, see .NET Core Support Policy.

不可用于 .NET Core 的第三方库或 NuGet 包Third-party libraries or NuGet packages not available for .NET Core

库很快将使用 .NET Standard。Libraries are quickly embracing .NET Standard. 通过 .NET Standard 可跨各种 .NET 实现(包括 .NET Core)共享代码。.NET Standard enables sharing code across all .NET implementations, including .NET Core. 使用 .NET Standard 2.0 则更简单:With .NET Standard 2.0, this is even easier:

  • API 曲面已变为更大。The API surface became much larger.

  • 它引入了 .NET Framework 兼容性模式。It introduced a .NET Framework compatibility mode.

    此兼容性模式允许 .NET Standard 和 .NET Core 项目引用 .NET Framework 库。This compatibility mode allows .NET Standard and .NET Core projects to reference .NET Framework libraries. 若要详细了解兼容性模式,请参阅 Announcing .NET Standard 2.0(宣布发布 .NET Standard 2.0)。To learn more about the compatibility mode, see Announcing .NET Standard 2.0.

仅在以下情况下需要使用 .NET Framework:库或 NuGet 包使用 .NET Standard 或 .NET Core 中不提供的技术。You need to use .NET Framework only in cases where the libraries or NuGet packages use technologies that aren't available in .NET Standard or .NET Core.

不可用于 .NET Core 的 .NET 技术.NET technologies not available for .NET Core

某些 .NET Framework 技术在 .NET Core 中不可用。Some .NET Framework technologies aren't available in .NET Core. 其中一些技术可能在更高版本的 .NET Core 中可用。Some of them might be available in later .NET Core releases. 但其他技术不会应用于 .NET Core 面向的新应用程序模式,因此可能永远不可用。Others don’t apply to the new application patterns targeted by .NET Core and may never be available. 以下列表显示无法在 .NET Core 中找到的最常见技术:The following list shows the most common technologies not found in .NET Core:

  • ASP.NET Web 窗体应用程序:ASP.NET Web 窗体仅在.NET Framework 中可用。ASP.NET Web Forms applications: ASP.NET Web Forms are only available in .NET Framework. ASP.NET Core 不能用于 ASP.NET Web 窗体。ASP.NET Core cannot be used for ASP.NET Web Forms. 目前没有将 ASP.NET Web 窗体引入 .NET Core 的计划。There are no plans to bring ASP.NET Web Forms to .NET Core.

  • ASP.NET 网页应用程序:ASP.NET 网页未包含在 ASP.NET Core 中。ASP.NET Web Pages applications: ASP.NET Web Pages aren't included in ASP.NET Core.

  • WCF 服务的实现。WCF services implementation. 虽然 WCF 客户端库可从 .NET Core 使用 WCF 服务,WCF 服务器实现目前只在 .NET Framework 上可用。Even when there’s a WCF client library to consume WCF services from .NET Core, WCF server implementation is currently only available in .NET Framework. 这种情况虽然不属于 .NET Core 当前计划,但将来会考虑这点。This scenario is not part of the current plan for .NET Core, but it’s being considered for the future.

  • 工作流相关的服务:Windows Workflow Foundation (WF)、工作流服务(WCF + 单个服务中的 WF)和 WCF Data Services(以前称为“ADO.NET Data Services”)仅在 .NET Framework 上可用。Workflow-related services: Windows Workflow Foundation (WF), Workflow Services (WCF + WF in a single service), and WCF Data Services (formerly known as "ADO.NET Data Services") are only available in .NET Framework. 目前没有将这些技术引入 .NET Core 的计划。There are no plans to bring these technologies to .NET Core.

  • 语言支持:.NET Core 目前支持 Visual Basic 和 F#,但不是所有项目类型都支持。Language support: Visual Basic and F# are currently supported in .NET Core, but not for all project types. 有关支持的项目模板列表,请参阅 dotnet new 的模板选项For a list of supported project templates, see Template options for dotnet new.

平台不支持 .NET CorePlatform doesn’t support .NET Core

某些 Microsoft 或第三方平台不支持 .NET Core。Some Microsoft or third-party platforms don’t support .NET Core. 某些 Azure 服务提供尚不可用于 .NET Core 的 SDK。Some Azure services provide an SDK not yet available for consumption on .NET Core. 这只是过渡情况,因为所有 Azure 服务都将使用 .NET Core。This is a transitional circumstance, as all of Azure services use .NET Core. 在此期间,可使用等效的 REST API(而不是客户端 SDK)。In the meantime, you can use the equivalent REST API instead of the client SDK.

请参阅See also