適用于伺服器應用程式的 .NET Core/5 + vs .NET Framework.NET Core/5+ vs. .NET Framework for server apps

有兩個支援的 .NET 組建可用於建立伺服器端應用程式:There are two supported .NET implementations for building server-side apps:

  • .NET Framework.NET Framework
  • .Net core/5 +,其中包括 .net core、.net 5 和更新版本。.NET Core/5+, which includes .NET Core, .NET 5, and later versions.

兩者共用許多相同的元件,而您可以在這兩個元件之間共用程式碼。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/5 +:Use .NET Core/5+ 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/5 + 無法使用的協力廠商 .NET 程式庫或 NuGet 套件。Your app uses third-party .NET libraries or NuGet packages not available for .NET Core/5+.
  • 您的應用程式會使用適用于 .NET Core/5 + 的 .NET Framework 技術。Your app uses .NET Framework technologies that aren't available for .NET Core/5+.
  • 您的應用程式使用不支援 .NET Core/5 + 的平臺。Your app uses a platform that doesn't support .NET Core/5+.

選擇 .NET Core/5 + 的時機When to choose .NET Core/5+

下列各節提供先前所述挑選 .NET Core/5 + 原因的詳細說明。The following sections give a more detailed explanation of the previously stated reasons for picking .NET Core/5+.

跨平台需求Cross-platform needs

如果您的應用程式 (web/服務) 需要在多個平臺上執行 (Windows、Linux 和 macOS) ,請使用 .NET Core/5 +。If your application (web/service) needs to run on multiple platforms (Windows, Linux, and macOS), use .NET Core/5+.

.NET Core/5 + 支援先前所述的作業系統作為您的開發工作站。.NET Core/5+ 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. 您也可以使用 Visual Studio Code,其在 macOS、Linux 和 Windows 上執行。You can also use Visual Studio Code, which runs on macOS, Linux, and Windows. Visual Studio Code 支援 .NET Core/5 +,包括 IntelliSense 和調試。Visual Studio Code supports .NET Core/5+, including IntelliSense and debugging. 大部分的協力廠商編輯器(例如 Sublime、Emacs 和 VI)都會使用 .NET Core/5 +。Most third-party editors, such as Sublime, Emacs, and VI, work with .NET Core/5+. 這些協力廠商編輯器會透過 Omnisharp 取得編輯器 IntelliSense。These third-party editors get editor IntelliSense using Omnisharp. 您也可以避免使用任何程式碼編輯器,並直接使用 .NET CLI(適用于所有支援的平臺)。You can also avoid any code editor and directly use the .NET CLI, available for all supported platforms.

微服務架構Microservices architecture

微服務架構可讓您跨越服務界限混用技術。A microservices architecture allows a mix of technologies across a service boundary. 這項技術混合可針對可與其他微服務或服務搭配使用的新微服務,逐步使用 .NET Core/5 +。This technology mix enables a gradual embrace of .NET Core/5+ 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/5 +,並使其適合用來裝載您的微服務。All these platforms support .NET Core/5+ 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. .NET Framework 可用於 Windows 容器,但 .NET Core/5 + 的模組化和輕量本質讓它成為容器的較佳選擇。.NET Framework can be used on Windows containers, but the modularity and lightweight nature of .NET Core/5+ makes it a better choice for containers. 當您建立和部署容器時,其映射的大小會比 .NET Framework 的 .NET Core/5 + 小很多。When creating and deploying a container, the size of its image is much smaller with .NET Core/5+ than with .NET Framework. 舉例來說,因為它是跨平台的,您可以將伺服器應用程式部署到 Linux Docker 容器。Because it's cross-platform, you can deploy server apps to Linux Docker containers, for example.

您可以在自己的 Linux 或 Windows 基礎結構中,或在 Azure Kubernetes Service 等雲端服務中裝載 Docker 容器。Docker containers can be hosted in your own Linux or Windows infrastructure, or in a cloud service such as Azure Kubernetes Service. Azure Kubernetes Service 可在雲端中管理、協調及調整容器型應用程式。Azure Kubernetes Service can manage, orchestrate, and scale container-based applications in the cloud.

高效能且可擴充的系統High-performance and scalable systems

當您的系統需要最佳效能和擴充性時,.NET Core/5 + 和 ASP.NET Core 是您的最佳選擇。When your system needs the best possible performance and scalability, .NET Core/5+ and ASP.NET Core are your best options. 適用于 Windows Server 和 Linux 的高效能伺服器執行時間讓 ASP.NET Core 在 TechEmpower 基準檢驗上執行的 web 架構。The high-performance server runtime for Windows Server and Linux makes ASP.NET Core 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/5 +。To install applications with dependencies on different versions of .NET, we recommend .NET Core/5+. 這項 .NET 的執行支援在同一部電腦上並存安裝不同版本的 .NET 執行時間。This implementation of .NET supports side-by-side installation of different versions of the .NET runtime on the same machine. 此並存安裝允許相同伺服器上的多個服務,每個服務都位於自己的 .NET Core/5 + 版本上。This side-by-side installation allows multiple services on the same server, each of them on its own version of .NET Core/5+. 它也會在應用程式升級和 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/5 + 針對新的應用程式和應用程式模式提供顯著的優點。.NET Core/5+ offers significant benefits for new applications and application patterns. 不過,.NET Framework 會持續成為許多現有案例的自然選擇,因此在所有伺服器應用程式中,.NET Framework 不會被 .NET Core/5 + 取代。However, .NET Framework continues to be the natural choice for many existing scenarios, and as such, .NET Framework isn't replaced by .NET Core/5+ for all server applications.

目前的 .NET Framework 應用程式Current .NET Framework applications

在大多數情況下,您不需要將現有的應用程式遷移至 .NET Core/5 +。In most cases, you don't need to migrate your existing applications to .NET Core/5+. 相反地,建議的方法是在擴充現有的應用程式時使用 .NET Core/5 +,例如在 ASP.NET Core 中撰寫新的 web 服務。Instead, a recommended approach is to use .NET Core/5+ as you extend an existing application, such as writing a new web service in ASP.NET Core.

.NET Core/5 + 無法使用的協力廠商程式庫或 NuGet 套件Third-party libraries or NuGet packages not available for .NET Core/5+

.NET Standard 可讓您在所有 .NET 執行(包括 .NET Core/5 +)之間共用程式碼。.NET Standard enables sharing code across all .NET implementations, including .NET Core/5+. 使用 .NET Standard 2.0 時,相容性模式可讓 .NET Standard 和 .NET Core/5 + 專案參考 .NET Framework 程式庫。With .NET Standard 2.0, a compatibility mode allows .NET Standard and .NET Core/5+ projects to reference .NET Framework libraries. 如需詳細資訊,請參閱 .NET Framework 程式庫的支援For more information, see Support for .NET Framework libraries.

只有當程式庫或 NuGet 套件使用 .NET Standard 或 .NET Core/5 + 中無法使用的技術時,才需要使用 .NET Framework。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/5+.

適用于 .NET Core/5 + 的 .NET Framework 技術.NET Framework technologies not available for .NET Core/5+

某些 .NET Framework 技術無法在 .NET Core/5 + 中使用。Some .NET Framework technologies aren't available in .NET Core/5+. 下列清單顯示 .NET Core/5 + 中找不到的最常見技術:The following list shows the most common technologies not found in .NET Core/5+:

  • ASP.NET Web Forms 的應用程式: ASP.NET Web Forms 僅適用于 .NET Framework。ASP.NET Web Forms applications: ASP.NET Web Forms are only available in .NET Framework. ASP.NET Core 無法用於 ASP.NET Web Forms。ASP.NET Core cannot be used for ASP.NET Web Forms.

  • ASP.NET Web Pages 應用程式:ASP.NET Web Pages 未隨附於 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/5 + 取用 wcf 服務,但 wcf 伺服器的執行目前只在 .NET Framework 中提供。Even when there's a WCF client library to consume WCF services from .NET Core/5+, WCF server implementation is currently only available in .NET Framework.

  • 工作流程相關的服務: Windows Workflow Foundation (WF) 、工作流程服務 (單一服務) 中的 WCF + WF,而 WCF Data Services (之前稱為「ADO.NET 資料服務」 ) 僅適用于 .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/5 + 中目前支援 Visual Basic 和 F #,但並非所有專案類型都支援。Language support: Visual Basic and F# are currently supported in .NET Core/5+, but not for all project types. 如需支援的專案範本清單,請參閱 dotnet new 的範本選項For a list of supported project templates, see Template options for dotnet new.

如需詳細資訊,請參閱 .net 5 中無法使用的 .NET Framework 技術For more information, see .NET Framework technologies unavailable in .NET 5.

平臺不支援 .NET Core/5 +Platform doesn't support .NET Core/5+

某些 Microsoft 或協力廠商平臺不支援 .NET Core/5 +。Some Microsoft or third-party platforms don't support .NET Core/5+. 某些 Azure 服務提供的 SDK 尚無法在 .NET Core/5 + 上使用。Some Azure services provide an SDK not yet available for consumption on .NET Core/5+. 在這種情況下,您可以使用對等的 REST API,而不是用戶端 SDK。In such cases, you can use the equivalent REST API instead of the client SDK.

另請參閱See also