dotNET Core Update from 3.1 to 6

Migrate ASP.NET Core Razor Project

In this paragraph, we will update the ASP.NET Core project from 3.1 to 6.

The following table shows that current LTS version status:

LTS Version Release Date EOS
.NET Core 2.1 2018-05-30 2021-08-21
.NET Core 3.1 2019-12-03 2020-12-13
.NET 6 2021-11-08 2024-11-12
.NET 8 2023-11 (projected) 2026-12 (projected)

Modify .csproj

The main topics in this step are:

  1. Change the target framework from .NET Core 3.1 to .NET 6.0.
  2. Update the NuGet packages (Microsoft.AspNetCore.*, Microsoft.EntityFrameworkCore.*, Microsoft.Extensions.*, and System.Net.Http.Json).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<PropertyGroup>
- <TargetFramework>netcoreapp3.1</TargetFramework>
+ <TargetFramework>net6.0</TargetFramework>
+ <Nullable>enable</Nullable>
+ <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
- <PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="3.1.6" />
- <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.6" />
- <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="3.1.6" />
- <PackageReference Include="System.Net.Http.Json" Version="3.2.1" />
+ <PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="6.0.0" />
+ <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0" />
+ <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0" />
+ <PackageReference Include="System.Net.Http.Json" Version="6.0.0" />
</ItemGroup>

Modify Program.cs

In this step, we need to merge Startup.cs into Program.cs, the common rules as following table:

.NET Core 3.1 .NET 6
configuration builder.Configuration
services builder.Services
env app.Environment

A basic example of Program.cs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRouting(options => options.LowercaseUrls = true);
builder.Services.AddRazorPages()
.AddRazorRuntimeCompilation();

builder.WebHost.ConfigureKestrel(options =>
{
// To listen for incoming http connection on port 5000
options.ListenAnyIP(5000);
// To listen for incoming https connection on port 5001
//options.ListenAnyIP(5001, configure => configure.UseHttps());
});


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapRazorPages());

app.Run();

/* Global Variable if needed */
#pragma warning disable IDE0040
partial class Program
{
public static List<string> GlobalStrings = new List<string>();
}
#pragma warning restore IDE0040

After merged, we can remove the Startup.cs from current project.

Migrate Ubuntu Runtime

Steps

Check the dotnet runtime

1
2
3
4
5
# If is SDK
$ dotnet --list-sdk

# If is runtime
$ dotnet --list-runtimes

(Optional) Remove .NET Core Previous Versions

  • Remove all version

    1
    2
    $ sudo apt -y remove dotnet-host
    $ sudo apt -y autoremove
  • Remove specific SDK version

    1
    $ sudo apt -y remove dotnet-sdk-3.1
  • Remove specific runtime version

    1
    2
    $ sudo apt -y remove dotnet-runtime-3.1
    $ sudo apt -y remove aspnetcore-runtime-3.1

Install .NET 6

  • The .NET SDK allows you to develop apps with .NET. If you install the .NET SDK, you don’t need to install the corresponding runtime.

    1
    2
    $ sudo apt update
    $ sudo apt -y install dotnet6
  • The ASP.NET Core Runtime allows you to run apps that were made with .NET that didn’t provide the runtime.

    1
    2
    $ sudo apt update
    $ sudo apt -y install aspnetcore-runtime-6.0
  • As an alternative to the ASP.NET Core Runtime, you can install the .NET Runtime, which doesn’t include ASP.NET Core support.

    1
    2
    $ sudo apt update
    $ sudo apt -y install dotnet-runtime-6.0

References