Microsoft.NET.Sdk 和 Microsoft.NET.Sdk.Web 有什么区别 [英] What are the differences between Microsoft.NET.Sdk and Microsoft.NET.Sdk.Web

查看:57
本文介绍了Microsoft.NET.Sdk 和 Microsoft.NET.Sdk.Web 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个宿主项目的解决方案(一个是 Web Host 另一个是 Generic Host) 和这两个宿主项目引用的类库项目.

I have a solution with two host projects (one is a Web Host and the other is a Generic Host) and a class library project referenced by those two host projects.

Sdk 属性中*.csproj 文件的根标签 ,宿主项目(web 宿主和通用宿主)都使用 Microsoft.NET.Sdk.Web,但是类库项目使用 Microsoft.NET.Sdk.

In the Sdk attribute of the root tag <Project> of the *.csproj file, both host projects (web host and generic host) are using Microsoft.NET.Sdk.Web, but the class library project uses the Microsoft.NET.Sdk.

这两个宿主项目引用了 Microsoft.AspNetCore.App 元包.

The two host projects references the Microsoft.AspNetCore.App metapackage.

类库项目正在使用 Microsoft.NETCore.App,但它单独引用了一些 ASP.NET Core 包(Microsoft.AspNetCore.App 的包不是在 Microsoft.NETCore.App 中).

The class library project are using Microsoft.NETCore.App, but it references some ASP.NET Core packages individually (packages of Microsoft.AspNetCore.App that are not in Microsoft.NETCore.App).

关于正确的 SDK 和元包:

About the correct SDK and metapackages:

1) 在通用宿主项目中,我是否应该使用纯 .NET Core(Microsoft.NET.SdkMicrosoft.NETCore.App>) 而不是 ASP.NET Core(Microsoft.NET.Sdk.WebMicrosoft.AspNetCore.App),因为它不是网络项目?

1) In the generic host project, should I use pure .NET Core (Microsoft.NET.Sdk and Microsoft.NETCore.App) instead of ASP.NET Core (Microsoft.NET.Sdk.Web and Microsoft.AspNetCore.App) since it is not a web project?

2) 在类库项目中,是否可以使用Microsoft.NET.SdkMicrosoft.AspNetCore.App 来避免引用不同版本的可能性?属于 Microsoft.AspNetCore.App 的包(以避免例如宿主项目中的 Microsoft.AspNetCore.App@2.1.0Microsoft.Extensions.类库项目中的Configuration@2.0.0)?或者我只能将 Microsoft.AspNetCore.App 元包与 Microsoft.NET.Sdk.Web SDK 一起使用?

2) In the class library project, is it fine to use Microsoft.NET.Sdk with Microsoft.AspNetCore.App to avoid the possibility to reference different versions of packages that belong to Microsoft.AspNetCore.App (to avoid for example, Microsoft.AspNetCore.App@2.1.0 in the host project and Microsoft.Extensions.Configuration@2.0.0 in the class library project)? Or I can only use Microsoft.AspNetCore.App metapackage with Microsoft.NET.Sdk.Web SDK?

3) 使用 Microsoft.NET.SdkMicrosoft.NET.Sdk.Web 有什么区别?docs"SDK,正如分层文档所描述的,是一组可以构建 .NET Core 代码的 MSBuild 任务和目标.",但为什么我们需要同时拥有它们?在实践中,Microsoft.NET.Sdk.Web 做什么而 Microsoft.NET.Sdk 没有?

3) What difference does it make to use Microsoft.NET.Sdk or Microsoft.NET.Sdk.Web? The docs says that "The SDK, as the layering document describes, is a set of MSBuild tasks and targets that can build .NET Core code.", but why do we need to have both of them? In practice, what Microsoft.NET.Sdk.Web does that Microsoft.NET.Sdk doesn't?

推荐答案

广告 (1) 和 (3):核心"SDK 和 Web SDK 之间有什么区别,它们如何影响通用主机应用程序?

Ad (1) and (3): What are the differences between the "core" and and web SDKs, how do these affect generic host apps?

最重要的区别是:

  1. 默认项目

对于要包含在已发布应用程序中的文件,Web SDK 具有不同的定义和通配模式.

The web SDK has different definitions and globbing patterns for which files to include in your published application.

例如当你有一个 appsettings.json 文件时,使用 web sdk 的项目将自动包含它,因为有模式确保 .config, .json 文件和 wwwroot 文件夹中的所有文件都是发布输出的一部分.请参阅 GitHub 上这些模式的 MSBuild 源代码.

E.g. when you have an appsettings.json file, projects using the web sdk will automatically include it since there are patterns in place that ensure that .config, .json files and all files in a wwwroot folder are all part of the publish output. See the MSBuild source code on GitHub for these patterns.

如果你有一个通用主机并且不使用Web SDK,你可能需要在csproj文件中添加代码来指定将哪些文件复制到发布目录(或者使用IDE将复制到输出目录"设置,其中还包括发布输出中的文件,但也会将它们复制到构建输出中):

If you have a generic host and don't use the Web SDK, you may need to add code to the csproj file to specify which files to copy to the publish directory (or use an IDE to change the "copy to output directory" setting which also includes files in the publish output but will also copy them to the build output):

<ItemGroup>
  <None Update="*.json" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>

  • 网络发布逻辑

    Web SDK 的另一个重要部分是它包含 Web 应用程序的部署逻辑.

    Another essential part of the Web SDK is that it contains the deployment logic for web applications.

    如果您计划使用发布配置文件(.pubxml 文件)或使用 MSBuild/MSDeploy 部署到 azure 或文件系统,您将需要此发布逻辑.

    If you plan to use publish profiles (.pubxml files) or deploy to azure or filesystems using MSBuild / MSDeploy, you will need this publishing logic.

    广告 (2):类库使用哪个 SDK?

    Ad (2): Which SDK to use for class libraries?

    为了在发布公共库时获得最大的兼容性(例如通过 NuGet),请使用核心 SDK 并引用尽可能低版本的单个包 - 例如2.1.0/2.1.1.

    For maximum compatibility when publishing public libraries (e.g. via NuGet), use the core SDK and reference individual packages with the lowest possible version - e.g. 2.1.0 / 2.1.1.

    如果您开发包含 razor 视图的类库,您将需要使用 Microsoft.NET.Sdk.Razor SDK 来获取 razor 工具(例如,当您使用 dotnet new razorclasslib 模板).

    If you develop a class library containing razor views, you will need to use the Microsoft.NET.Sdk.Razor SDK to get razor tooling (e.g. when you use the dotnet new razorclasslib template).

    对于您希望使用与应用程序相同的元包引用的库和测试项目,目前情况有点复杂,但会变得更好:

    For libraries and test projects where you want to use the same meta package reference as the application, things are a bit complicated at the moment but it's going to get better:

    对于 ASP.NET Core 2.1 工具(!) (CLI 2.1.*),我建议对类库使用非 Web SDK,并使用该包的 2.1.1 版.永远不要升级它,即使 NuGet 为您提供升级.

    For ASP.NET Core 2.1 tools(!) (CLI 2.1.*), I suggest using the non-web SDK for class libraries and use the version 2.1.1 of that package. Don't ever upgrade it, even if NuGet offers you an upgrade.

    对于 2.1 工具(!)(CLI 2.1.*)中的测试项目,它有点不同和棘手,请参阅 集成和单元测试不再适用于无法在运行时找到程序集的 ASP.NET Core 2.1

    For test projects in 2.1 tools(!) (CLI 2.1.*), it is a bit different and tricky, see Integration and unit tests no longer work on ASP.NET Core 2.1 failing to find assemblies at runtime

    从 2.2 工具 (CLI 2.2.100+) 开始,对 ASP.NET Core 元包的无版本包引用移至核心 SDK,因此您可以为 ASP.NET Core 2.1 和 2.2 开发库和测试项目使用核心"SDK(前提是您使用工具 2.2.100+)使用无版本包引用:

    Beginning in 2.2 tools (CLI 2.2.100+), the version-less package references to ASP.NET Core metapackages are moved to the core SDK so you can develop libraries and test projects for both ASP.NET Core 2.1 and 2.2 using the ""core"" SDK (provided you use tools 2.2.100+) using version-less package references:

    <ItemGroup>
      <PackageReference Include="Microsoft.AspNetCore.App" />
    </ItemGroup>
    

    在 .NET Core/ASP.NET Core 3.0 中,您将能够通过完全新的机制(不需要 web-SDK)来引用框架:

    In .NET Core / ASP.NET Core 3.0, you will be able to reference the framework via a new mechanism altogether (no web-SDK needed):

    <ItemGroup>
      <FrameworkReference Include="Microsoft.AspNetCore.App" />
    </ItemGroup>
    

    这篇关于Microsoft.NET.Sdk 和 Microsoft.NET.Sdk.Web 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆