Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can netcoreapp3.1 be built by .NET 5?

Tags:

c#

.net

I am building an Azure Function project, so I cannot target .NET 5. Instead, my project is a netcoreapp3.1. However, when my project is built, anytime there is some information printed, I see dotnet 5 being mentioned. Example:

##[warning]/usr/share/dotnet/sdk/5.0.101/Microsoft.Common.CurrentVersion.targets(2123,5): Warning MSB3245: Could not resolve this reference. Could not locate the assembly "System.Text.Json, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.

(The issue that I'm having with System.Text.Json is a problem of its own, in this post I'd like to understand how .NET 5 can build netcoreapp3.1).

I have .NET Core 3.1 installed on my system next to .NET 5. Why doesn't the build system use 3.1? I believe that .NET 5 has some breaking changes in comparison to 3.1, so I am not sure if this is OK to use .NET 5 to build the project.

The same thing happens both in Azure DevOps Pipeline builds, and locally. Locally, in Rider, I see also this when I Build:

CONSOLE: Use build tool: /usr/share/dotnet/sdk/5.0.102/MSBuild.dll

Here's the result of ll /usr/share/dotnet/sdk:

drwxr-xr-x 28 root root  12K sty 13 08:24 3.1.405/
drwxr-xr-x 28 root root  12K sty 13 08:24 5.0.102/

As you can see, I have both 3.1 and 5.

like image 456
mnj Avatar asked Jan 19 '21 14:01

mnj


2 Answers

You need to separate three things:

  • the build SDK being used to perform the build steps
  • the target platform which defines the API features available in the core packages (used by the build SDK)
  • the runtime platform being used to actually execute your application, which is expected to support the target platform's API features

In this case, the build SDK is 5, and the target platform is 3.1; that's fine - build SDK 5 knows all about .NET Core 3.1 in terms of target features, so it knows what to do. As long as the runtime is at least .NET Core 3.1, it should all be fine (if we presume that nobody makes breaking changes to the core libraries in some future runtime environment).

The fact that the build SDK is v5 is interesting but largely irrelevant - they could have given the build SDK completely different version numbers and everything would still work. It is just useful to keep them in sync, so you know what is what.

If you really want to use a specific build SDK rather than the default (which largely means: the highest): that's what global.json can enable for you.

like image 87
Marc Gravell Avatar answered Sep 18 '22 22:09

Marc Gravell


There's a difference between the .NET Core version Visual Studio (or Rider) uses for building the project and the version used for running the target application. By default, the latest installed version is used for building, and it runs against the version specified with the <TargetFramework> entry in the .csproj file. If the build works fine with .NET Core 5.0, I would stick with that, because it allows use of some advanced features even if targetting the old runtime.

If you have problems with 5.0 (there were some breaking changes even in the build infrastructure), you can create a file named global.json in the project root directory, telling the build system which SDK to use.

This global.json file ensures that the build uses the latest .NET Core 3.1 version:

{
  "sdk": {
    "version": "3.1.101",
    "rollForward": "minor"
  }
}
like image 33
PMF Avatar answered Sep 20 '22 22:09

PMF