How To Upgrade a .NET Core 3.1 project to .NET 6.0

.NET is 20 years old this week and I’ve been using it for a big chunk of that. It’s come a long way since the early days when I started and is now cross-platform.

However, I’ve diversified my tech stacks and moved away from .NET a bit. I now do a lot of work in Python/Django and JavaScript. The .NET tooling stills feels a little second-class when not using Windows and I mainly use Linux these days.

Some things Microsoft does get right are backwards compatibility and long term support. There are lots of problems with Python versioning or the latest JavaScript framework of the week, whereas .NET has been supported for a long time. However, sometimes you do need to update.

Why

.NET Core 3.1 LTS support ends on the 3rd of December this year (2022) and .NET 5.0 on the 8th of May this year. .NET 6.0 LTS is supported until the 8th of November 2024 so you will need to update to this in order to continue getting security patches.

You may also want to update the version of C# in use to take advantage of new features and this is a good time to do it. Here is how I updated my Huxley 2 railway data proxy from .NET Core 3.1 LTS to .NET 6.0 LTS (including for the Docker container and Azure pipelines support) and from C# 8 to C# 10.

How

You will need to edit your .csproj project files to upgrade the framework and language. If you use Docker or Azure Pipelines then you will need to update the configuration files for these too.

Make sure that you have the latest version of the .NET SDK installed. You can download it from dotnet.microsoft.com.

Project Files

Update your .csproj XML files. Change the TargetFramework tag content from e.g. netcoreapp3.1 to net6.0 so that it looks like this:

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

Other tags have been omitted for clarity. Don’t forget to do this for all of your project files, including unit test projects.

You may also want to update the C# version to use new language features. Change the LangVersion tag content from e.g. 8.0 to 10.0 so that it looks like this:

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <LangVersion>10.0</LangVersion>
</PropertyGroup>

Save the files, rebuild everything, run all the automated tests and give it a good manual test to make sure everything is working.

Docker

If you use Docker then you will need to update your Dockerfile to use the latest images. If you are using ASP.NET Core then change e.g. mcr.microsoft.com/dotnet/core/sdk:3.1 to mcr.microsoft.com/dotnet/sdk:6.0 for the SDK and e.g. mcr.microsoft.com/dotnet/core/aspnet:3.1 to mcr.microsoft.com/dotnet/aspnet:6.0 for the runtime so that it looks like this:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env

FROM mcr.microsoft.com/dotnet/aspnet:6.0

Other lines have been omitted for clarity. Note the removal of core from the path.

Azure Pipelines

If you use YAML Azure Pipelines then you will need to update the azure-pipelines.yml file. Change the first task to install the latest patch version of the .NET 6.0 SDK by changing version from 3.1.x to 6.0.x so that it looks like this:

stages:
- stage: Build_and_test_stage
  jobs:
  - job: Build_and_test_job
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - task: UseDotNet@2
      displayName: "Use .NET 6.0 SDK"
      env:
        DOTNET_CLI_TELEMETRY_OPTOUT: true
        DOTNET_NOLOGO: true
      inputs:
        packageType: 'sdk'
        version: '6.0.x'

The build, test and publish tasks etc. shouldn’t need any changes but the final deploy task will if you are hosting on the Azure web app service. Change RuntimeStack from DOTNETCORE|3.1 to DOTNETCORE|6.0 so that it looks like this:

    - task: AzureRmWebAppDeployment@4
      displayName: 'Deploy Huxley 2 to Azure web app service'
      inputs:
        RuntimeStack: 'DOTNETCORE|6.0'

Again, other lines have been omitted for clarity.

GitHub Actions is similar (it shares a lot with Azure Pipelines) but it is a little different. Have a look at the dotnet-version: 6.0.x line in the Huxley 2 CI config.

Happy upgrades and happy birthday .NET!


This blog is treeware! If you found it useful then please plant a tree.
Donate a treeDonate a tree🌳🌳