Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between Microsoft.NET.Sdk and Microsoft.NET.Sdk.Web

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.

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.

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

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).

About the correct SDK and metapackages:

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) 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, [email protected] in the host project and [email protected] in the class library project)? Or I can only use Microsoft.AspNetCore.App metapackage with Microsoft.NET.Sdk.Web 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?

like image 633
lmcarreiro Avatar asked Sep 20 '18 23:09

lmcarreiro


People also ask

What is Microsoft NET SDK Web?

NET. Sdk. Web is an MSBuild project SDK for building ASP.NET Core apps. It's possible to build an ASP.NET Core app without this SDK, however, the Web SDK is: Tailored towards providing a first-class experience.

What is the difference between .NET SDK and .NET runtime?

The SDK is all of the stuff that is needed/makes developing a . NET Core application easier, such as the CLI and a compiler. The runtime is the "virtual machine" that hosts/runs the application and abstracts all the interaction with the base operating system.

What is project SDK Microsoft NET SDK?

. NET Core and . NET 5 and later projects are associated with a software development kit (SDK). Each project SDK is a set of MSBuild targets and associated tasks that are responsible for compiling, packing, and publishing code. A project that references a project SDK is sometimes referred to as an SDK-style project.

What is the use of Microsoft NET Core SDK?

The . NET SDK is a set of libraries and tools for developing and running . NET applications. It includes the MSBuild build engine, the Roslyn (C# and Visual Basic) compiler, and the F# compiler.


1 Answers

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

The most important differences are:

  1. Default Items

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

    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.

    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> 
  2. Web Publish logic

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

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

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

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.

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:

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.

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

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> 

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> 
like image 196
Martin Ullrich Avatar answered Oct 12 '22 23:10

Martin Ullrich