套件、中繼套件和架構Packages, metapackages and frameworks

.NET Core 是由 NuGet 套件構成的平台。.NET Core is a platform made of NuGet packages. 有些產品體驗可獲益於細部定義的套件,有些則更適合廣泛定義的套件。Some product experiences benefit from fine-grained definition of packages while others from coarse-grained. 為了滿足這類雙重特性,產品會以細部套件組的形式及廣泛區塊的形式來散發,而這類廣泛區塊的套件類型俗稱為中繼套件To accommodate this duality, the product is distributed as a fine-grained set of packages and in coarser chunks with a package type informally called a metapackage.

每個 .NET Core 套件可支援在多個 .NET 實作 (以架構來代表) 上執行。Each of the .NET Core packages support being run on multiple .NET implementations, represented as frameworks. 這些架構有些是傳統的架構,例如 net46 即表示 .NET Framework。Some of those frameworks are traditional frameworks, like net46, representing the .NET Framework. 其他還有可以視為「套件型架構」的全新架構,該架構可以建立新的模型來定義架構。Another set is new frameworks that can be thought of as "package-based frameworks", which establish a new model for defining frameworks. 這些套件型架構完全是以套件形式構成與定義,並在套件和架構之間形成強式關聯。These package-based frameworks are entirely formed and defined as packages, forming a strong relationship between packages and frameworks.


.NET Core 分割成各個套件組,以提供基本、較高層級的資料類型、應用程式組合類型與一般公用程式。.NET Core is split into a set of packages, which provide primitives, higher-level data types, app composition types and common utilities. 其中的每個套件皆代表具有相同名稱的單一組件。Each of these packages represent a single assembly of the same name. 例如,System.Runtime 包含 System.Runtime.dll。For example, System.Runtime contains System.Runtime.dll.

以細部方式定義的套件有下列許多優點:There are advantages to defining packages in a fine-grained manner:

  • 細部套件可以按照自己的排程出貨,且僅會對其他套件進行相對有限的測試。Fine-grained packages can ship on their own schedule with relatively limited testing of other packages.
  • 細部套件可以提供不同的作業系統和 CPU 支援。Fine-grained packages can provide differing OS and CPU support.
  • 細部套件可以僅具有特定一個程式庫的專屬相依性。Fine-grained packages can have dependencies specific to only one library.
  • 由於未參考的套件不會跟應用程式一起散發,因此應用程式比較小。Apps are smaller because unreferenced packages don't become part of the app distribution.

其中有些優勢只適用於特定情況。Some of these benefits are only used in certain circumstances. 比方說,.NET Core 套件的出貨時間通常與相同平台支援的排程相同。For example, NET Core packages will typically ship on the same schedule with the same platform support. 需要進行服務時,修正程式可以透過小型單一套件更新的形式來散發和安裝。In the case of servicing, fixes can be distributed and installed as small single package updates. 由於變更的範圍較窄,修正程式的驗證和提供時間可以僅限於滿足單一程式庫的需求。Due to the narrow scope of change, the validation and time to make a fix available is limited to what is needed for a single library.

以下是 .NET Core 的主要 NuGet 套件清單:The following is a list of the key NuGet packages for .NET Core:

一般來說,包含中繼套件比包含每個套件更簡單且穩固。Typically, rather than including each package, it's easier and more robust to include a metapackage. 不過,當您需要單一套件時,您可以如下列範例所示將它加入,該範例參考 System.Runtime (英文) 套件。However, when you need a single package, you can include it as in the following example, which references the System.Runtime package.

<Project Sdk="Microsoft.NET.Sdk">
    <PackageReference Include="System.Runtime" Version="4.3.0" />


中繼套件一種 NuGet 套件慣例,其用來描述可合理搭配的一組套件。Metapackages are a NuGet package convention for describing a set of packages that are meaningful together. 中繼套件可將套件設為具有相依性,以代表這組套件。They represent this set of packages by making them dependencies. 中繼套件也可以選擇性地指定架構,以為這組套件建立架構。They can optionally establish a framework for this set of packages by specifying a framework.

根據預設,舊版的 .NET Core 工具 (project.json 和 csproj 工具) 已指定架構和中繼套件。Previous versions of the .NET Core tools (both project.json and csproj-based tools) by default specified both a framework and a metapackage. 不過目前,目標架構會隱含參考中繼套件,因此每個中繼套件都會繫結至目標架構。Currently, however, the metapackage is implicitly referenced by the target framework, so that each metapackage is tied to a target framework. 例如,netstandard1.6 架構會參考 NetStandard.Library 1.6.0 版的中繼套件。For example, the netstandard1.6 framework references the NetStandard.Library version 1.6.0 metapackage. 同樣地,netcoreapp2.1 架構會參考 Microsoft.NETCore.App 2.1.0 版的中繼套件。Similarly, the netcoreapp2.1 framework references the Microsoft.NETCore.App Version 2.1.0 metapackage. 如需詳細資訊,請參閱 Implicit metapackage package reference in the .NET Core SDK (.NET Core SDK 中的隱含中繼套件參考)。For more information, see Implicit metapackage package reference in the .NET Core SDK.

設定架構目標並隱含參考中繼套件,表示您實際上是以單一動作來新增每個相依套件的參考。Targeting a framework and implicitly referencing a metapackage means that you in effect are adding a reference to each of its dependent packages as a single gesture. 這會讓這些套件中的所有程式庫都可供 IntelliSense (或類似體驗) 使用,以及用於發行您的應用程式。That makes all of the libraries in those packages available for IntelliSense (or similar experience) and for publishing your app.

使用中繼套件的優點如下:There are advantages to using metapackages:

  • 讓參考大量細部套件的使用者體驗更方便。Provides a convenient user experience to reference a large set of fine-grained packages.
  • 其中所定義的套件組 (包括特定版本),均經過測試且適合共同運作。Defines a set of packages (including specific versions) that are tested and work well together.

.NET Standard 中繼套件是:The .NET Standard metapackage is:

  • NETStandard.Library - 描述 ".NET Standard" 中所包含的程式庫。NETStandard.Library - Describes the libraries that are part of the ".NET Standard". 適用於所有支援 .NET Standard 的 .NET 實作 (例如,.NET Framework、.NET Core 和 Mono)。Applies to all .NET implementations (for example, .NET Framework, .NET Core and Mono) that support .NET Standard. 可建立 'netstandard' 架構。Establishes the 'netstandard' framework.

主要的 .NET Core 中繼套件包括:The key .NET Core metapackages are:


每個 .NET Core 套件都會支援一組執行階段架構。.NET Core packages each support a set of runtime frameworks. 架構也說明了具有特定架構目標時可以仰賴的可用 API 集 (和其他可能的特性)。Frameworks describe an available API set (and potentially other characteristics) that you can rely on when you target a given framework. 當有新的 API 加入時,就會為其建立版本。They are versioned as new APIs are added.

例如,System.IO.FileSystem 支援下列架構:For example, System.IO.FileSystem supports the following frameworks:

  • .NETFramework,Version=4.6.NETFramework,Version=4.6
  • .NETStandard,Version=1.3.NETStandard,Version=1.3
  • 6 Xamarin 平台 (例如 xamarinios10)6 Xamarin platforms (for example, xamarinios10)

您可以比較上述前兩個架構,因為它們是兩個不同的架構定義方式範例。It is useful to contrast the first two of these frameworks, since they are examples of the two different ways that frameworks are defined.

.NETFramework,Version=4.6 架構代表 .NET Framework 4.6 中可用的 API。The .NETFramework,Version=4.6 framework represents the available APIs in the .NET Framework 4.6. 您可以產生以 .NET Framework 4.6 參考組件編譯的程式庫,然後在 NuGet 套件的 net46 lib 資料夾中發佈這些程式庫。You can produce libraries compiled with the .NET Framework 4.6 reference assemblies and then distribute those libraries in NuGet packages in a net46 lib folder. 它將用於目標為 .NET Framework 4.6 或與其相容的應用程式。It will be used for apps that target the .NET Framework 4.6 or that are compatible with it. 這是所有架構的傳統運作方式。This is how all frameworks have traditionally worked.

.NETStandard,Version=1.3 架構是以套件為基礎的架構。The .NETStandard,Version=1.3 framework is a package-based framework. 它需仰賴以架構為目標的套件,並根據該架構來定義並公開 API。It relies on packages that target the framework to define and expose APIs in terms of the framework.

以套件為基礎的架構Package-based frameworks

架構和套件之間沒有雙向關聯性。There is a two-way relationship between frameworks and packages. 第一個部分要定義特定架構可用的 API,例如 netstandard1.3The first part is defining the APIs available for a given framework, for example netstandard1.3. 目標為 netstandard1.3 (或 netstandard1.0 這類相容架構) 的套件會定義 netstandard1.3 可用的 API。Packages that target netstandard1.3 (or compatible frameworks, like netstandard1.0) define the APIs available for netstandard1.3. 這乍聽之下像是循環定義,其實不然。That may sound like a circular definition, but it isn't. 由於是「以套件為基礎」,因此架構的 API 定義也來自套件。By virtue of being "package-based", the API definition for the framework comes from packages. 架構本身並不會定義任何 API。The framework itself doesn't define any APIs.

關聯性的第二個部分是資產的選取項目。The second part of the relationship is asset selection. 套件可以包含多個架構的資產。Packages can contain assets for multiple frameworks. 參考一組套件及/或中繼套件時,需要依據架構來判斷應該選取哪些資產,例如 net46netstandard1.3Given a reference to a set of packages and/or metapackages, the framework is needed to determine which asset should be selected, for example net46 or netstandard1.3. 請務必選取正確的資產。It is important to select the correct asset. 例如,net46 資產不太可能相容於 .NET Framework 4.0 或 .NET Core 1.0。For example, a net46 asset is not likely to be compatible with .NET Framework 4.0 or .NET Core 1.0.

您可以在下圖中看到此關聯性。You can see this relationship in the following image. API 會以「架構」為目標,並加以定義。The API targets and defines the framework. 「架構」可用來「選取資產」。The framework is used for asset selection. 「資產」可提供 API。The asset gives you the API.


搭配 .NET Core 使用的主要套件型架構有以下兩個:The two primary package-based frameworks used with .NET Core are:

  • netstandard
  • netcoreapp

.NET Standard.NET Standard

.NET Standard (目標 Framework Monikernetstandard) 架構代表由 .NET Standard 所定義並建立於其上的 API。The .NET Standard (Target Framework Moniker: netstandard) framework represents the APIs defined by and built on top of the .NET Standard. 要在多個執行階段上執行的程式庫應以此架構為目標。Libraries that are intended to run on multiple runtimes should target this framework. 任何與 .NET Standard 相容的執行階段皆可支援這類程式庫,例如 .NET Core、.NET Framework 和 Mono/Xamarin。They will be supported on any .NET Standard compliant runtime, such as .NET Core, .NET Framework and Mono/Xamarin. 其中每個執行階段都支援一組 .NET Standard 版本,這取決於它們實作哪些 API 而定。Each of these runtimes supports a set of .NET Standard versions, depending on which APIs they implement.

netstandard 架構會隱含參考 NETStandard.Library 中繼套件。The netstandard framework implicitly references the NETStandard.Library metapackage. 例如,下列 MSBuild 專案檔指出專案是以 netstandard1.6 為目標,而該版本參考 NETStandard.Library 1.6 版的中繼套件。For example, the following MSBuild project file indicates that the project targets netstandard1.6, which references the NETStandard.Library version 1.6 metapackage.

<Project Sdk="Microsoft.NET.Sdk">

不過,專案檔中的架構和中繼套件參考不需要相符,而且您可以使用專案檔中的 <NetStandardImplicitPackageVersion> 項目,指定低於中繼套件版本的架構版本。However, the framework and metapackage references in the project file do not need to match, and you can use the <NetStandardImplicitPackageVersion> element in your project file to specify a framework version that is lower than the metapackage version. 例如,以下是有效的專案檔。For example, the following project file is valid.

<Project Sdk="Microsoft.NET.Sdk">

目標為 netstandard1.3 卻使用 NETStandard.Library 1.6.0 版,看似不合理,It may seem strange to target netstandard1.3 but use the 1.6.0 version of NETStandard.Library. 但這是有效的使用案例,因為中繼套件仍支援舊版的 netstandardIt is a valid use-case, since the metapackage maintains support for older netstandard versions. 如果您已經在 1.6.0 版中繼套件上進行標準化,並將其用於所有程式庫,且這些程式庫的目標為各種 netstandard 版本時,就可能發生這種情況。It could be the case you've standardized on the 1.6.0 version of the metapackage and use it for all your libraries, which target a variety of netstandard versions. 使用這個方法時,您只需要還原 NETStandard.Library 1.6.0 而不需還原舊版。With this approach, you only need to restore NETStandard.Library 1.6.0 and not earlier versions.

若是使用 NETStandard.Library 1.3.0 版但目標為 netstandard1.6,這種反向情況無效。The reverse would not be valid: targeting netstandard1.6 with the 1.3.0 version of NETStandard.Library. 由於較低版本的中繼套件不會公開較高架構的任何資產,因此使用較低的中繼套件時,您不能將目標設為更高的架構。You cannot target a higher framework with a lower metapackage, since the lower version metapackage will not expose any assets for that higher framework. 中繼套件的版本配置會判斷提示中繼套件應符合所描述架構的最高版本。The versioning scheme for metapackages asserts that metapackages match the highest version of the framework they describe. 由於版本配置的特性,假設第一個 NETStandard.Library 版本包含 netstandard1.6 資產,該版本即為 v1.6.0。By virtue of the versioning scheme, the first version of NETStandard.Library is v1.6.0 given that it contains netstandard1.6 assets. 上述範例使用 v1.3.0 是為了在範例中保持對稱,但實際上不存在這種情況。v1.3.0 is used in the example above, for symmetry with the example above, but does not actually exist.

.NET Core 應用程式.NET Core application

.NET Core (目標 Framework Monikernetcoreapp) 架構代表套件與相關聯的 API (隨附於 .NET Core 散發套件與它所提供的主控台應用程式模型)。The .NET Core (Target Framework Moniker: netcoreapp) framework represents the packages and associated APIs that come with the .NET Core distribution and the console application model that it provides. 由於目標為主控台應用程式模型,因此 .NET Core 應用程式必須使用此架構,包括僅限於 .NET Core 中執行的程式庫也應如此。.NET Core apps must use this framework, due to targeting the console application model, as should libraries that intended to run only on .NET Core. 使用此架構時,可限制應用程式和程式庫僅在 .NET Core 中執行。Using this framework restricts apps and libraries to running only on .NET Core.

Microsoft.NETCore.App 中繼套件是以 netcoreapp 架構為目標。The Microsoft.NETCore.App metapackage targets the netcoreapp framework. 它提供約 ~60 種程式庫的存取權,其中 NETStandard.Library 套件提供約 ~40 種,另外再加上 ~20 多種。It provides access to ~60 libraries, ~40 provided by the NETStandard.Library package and ~20 more in addition. 若要存取其他 API,您可以參考其他目標為 netcoreapp 或相容架構的程式庫,例如 netstandardYou can reference additional libraries that target netcoreapp or compatible frameworks, such as netstandard, to get access to additional APIs.

大部分由 Microsoft.NETCore.App 所提供的其他程式庫也會以 netstandard 為目標 (在其他 netstandard 程式庫滿足其相依性的情況下)。Most of the additional libraries provided by Microsoft.NETCore.App also target netstandard given that their dependencies are satisfied by other netstandard libraries. 這表示 netstandard 程式庫也可以將這些套件做為相依性參考。That means that netstandard libraries can also reference those packages as dependencies.