简体   繁体   English

.NET Core 2.2中的Metapackage和SDK有什么区别?

[英]What is the difference between Metapackage and SDK in .NET Core 2.2?

I am trying to make distinction between these two concepts in .NET Core terminology. 我试图在.NET Core术语中区分这两个概念。 I will try to illustrate my confusion with an example. 我将通过一个例子来说明我的困惑。

When I create a new class library project (eg: dotnet new classlib -o myclasslib ) the generated .csproj file looks like this: 当我创建一个新的类库项目(例如: dotnet new classlib -o myclasslib )时,生成的.csproj文件如下所示:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
</Project>

So far so good. 到现在为止还挺好。 Now, if I try to add WebApi controller class in this project (eg. for the purpose of dynamically loading plugins in my main WebApi application created as: dotnet new webapi -o mywebapi ) I would need to use things like ControllerBase class and [ApiController] and [HttpGet] attributes. 现在,如果我尝试在此项目中添加WebApi控制器类(例如,为了在我的主WebApi应用程序中动态加载插件,创建方式为: dotnet new webapi -o mywebapi ),则需要使用ControllerBase类和[ApiController][HttpGet]属性。 To keep things simple I just derive MyController from ControllerBase like this: 为了简单起见,我只是从ControllerBase派生MyController,如下所示:

using System;

namespace myclasslib
{
    public class MyController : Microsoft.AspNetCore.Mvc.ControllerBase
    {
    }
}

Trying to build this with dotnet build I get error: 尝试使用dotnet build构建它,但出现错误:

error CS0234: The type or namespace name 'AspNetCore' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) 错误CS0234:类型或名称空间名称“ AspNetCore”在名称空间“ Microsoft”中不存在(您是否缺少程序集引用?)

That's kind of expected because I created classlib project, but if change SDK in .csproj to Sdk="Microsoft.NET.Sdk.Web" and also change TargetFramework to netcoreapp2.2 (hoping to resolve the reference to ControllerBase class) like this: 这是预料之中的,因为我创建了classlib项目,但是如果将.csproj中的SDK更改为Sdk="Microsoft.NET.Sdk.Web" ,并将TargetFramework更改为netcoreapp2.2 (希望解析对ControllerBase类的引用),如下所示:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>
</Project>

I get the same error when building the project. 构建项目时出现相同的错误。 Isn't SDK supposed to include everything I need to build the project? SDK是否不应该包含构建项目所需的一切?

If I create usual webapi project (eg. dotnet new webapi -o mywebapi ) the generated .csproj looks like this: 如果我创建常规的webapi项目(例如dotnet new webapi -o mywebapi ),则生成的.csproj如下所示:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
  </ItemGroup>

</Project>

I notice the SDK is same as the one I set, but there is also a metapackage added: <PackageReference Include="Microsoft.AspNetCore.App" /> 我注意到该SDK与我设置的SDK相同,但是还添加了一个metapackage: <PackageReference Include="Microsoft.AspNetCore.App" />

So why do we need to explicitly add metapackage if we already specified that we want to use Microsoft.NET.Sdk. 因此,如果我们已经指定要使用Microsoft.NET.Sdk,为什么还要显式添加元包。 Web ? 网络

One additional question: what version of metapackage is used if we don't specify one in PackageReference (like in this generated webapi .csproj)? 另一个问题:如果未在PackageReference中指定版本,则使用哪个版本的metapackage(例如在此生成的webapi .csproj中)?

The SDK is just the build tooling and the .NET Core framework itself. SDK只是构建工具和.NET Core框架本身。 ASP.NET Core is a set of NuGet packages. ASP.NET Core是一组NuGet程序包。 Essentially .NET Core framework != ASP.NET Core code. 本质上是.NET Core框架!= ASP.NET Core代码。 The concept of metapackages is only tangentially related. 元包的概念仅与切向相关。 What could be called "ASP.NET Core" is actually dozens of individual NuGet packages. 所谓的“ ASP.NET Core”实际上是数十个单独的NuGet程序包。 You could reference each individually, but as you can imagine, that would be both tiresome and error prone. 可以分别引用每个对象,但是可以想象,这既麻烦又容易出错。 Metapackages are essentially a NuGet package that depends on multiple other NuGet packages. 元包本质上是一个NuGet包,它依赖于多个其他NuGet包。

By pulling in just the metapackage, therefore, essentially all of that metapackage's dependencies are also pull in. As a result, you can simply add a package reference for Microsoft.AspNetCore.App and you're off to the races. 通过拉动在刚刚元数据包,因此,基本上所有的元数据包的依赖拉。其结果是,你可以简单地添加了一个装基准Microsoft.AspNetCore.App ,你是去比赛。 However, the downside to that is that you're potentially getting dependencies that you don't actually need. 但是,这样做的缺点是,您可能会获得实际上不需要的依赖项。 That's not that big of an issue with something like a web app, because the dependencies can be trimmed, but a class library should not have excess dependencies. Web应用程序之类的问题并不是什么大问题,因为可以减少依赖关系,但类库不应具有过多的依赖关系。 As such, you should only reference individual NuGet packages you actually need from the Microsoft.AspNetCore namespace. 因此,您应该仅从Microsoft.AspNetCore命名空间引用您实际需要的单个NuGet程序包。

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

相关问题 NuGet 元包是否仅保留给 .net 核心? - Is NuGet metapackage reserved to .net core only? .NET Core和PCL有什么区别? - What's the difference between .NET Core and PCLs? “.NET Core”和“.NET Core App”有什么区别? - What is the difference between `.NET Core` and `.NET Core App`? 当前的 .NET SDK 不支持面向 .NET Core 2.2 - The current .NET SDK does not support targeting .NET Core 2.2 ASP.NET Core 中的 File()、PhysicalFile()、PhysicalFileResult() 有什么区别? - What is the difference between File(), PhysicalFile(), PhysicalFileResult() in ASP.NET Core? asp.net core 中的 Host 和 WebHost 类有什么区别 - What is the difference between Host and WebHost class in asp.net core Asp.Net Core中的IRequestCultureFeature和CurrentCulture有什么区别? - What's the difference between IRequestCultureFeature and CurrentCulture in Asp.Net Core? HttpContext.Request 和 Request a .NET Core controller 有什么区别? - What is the Difference between HttpContext.Request and Request a .NET Core controller? ASP.NET核心2.2:在框架类之间共享特定于请求的数据最安全的方法是什么? - ASP.NET core 2.2: what is the safest way of sharing request-specific data between framework classes? .NET 核心的 Windows 和 Linux 之间的编组有什么区别? - What is the difference in Marshaling between Windows and Linux for .NET Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM