Home Upgrade a Microservice from .NET Core 3.1 to .NET 5.0
Post
Cancel

Upgrade a Microservice from .NET Core 3.1 to .NET 5.0

Microsoft released the next major release, called .NET 5.0 which succeeds .NET Core 3.1. .NET 5.0 comes with a lot of improvements and also with C# 9. It is also the first step to a unified .NET platform and is the first version of Microsoft’s new release cycle. From now on, Microsoft will release a new version of .NET every November. .NET 6.0 will be released in November 2021, .NET 7.0 in November 2022, and so on.

Today, I want to show how to upgrade a microservice and its build pipeline from .NET Core 3.1 to .NET 5.0. You can find the code of this demo on GitHub.

This post is part of “Microservice Series - From Zero to Hero”.

System Requirements for .NET 5.0

To use .NET 5.0 you have to install the .NET 5.0 SDK from the dotnet download page and Visual Studio 16.8 or later.

Uprgrade from .NET Core 3.1 to .NET 5.0

To upgrade your solution to .NET 5.0, you have to update the TargetFramework in every .csproj file of your solution. Replace

with

Instead of updating all project files and next year updating them again, I created a new file called common.props in the root folder of the solution. This file contains the following code:

This file defines the C# version I am using and sets DefaultTargetFramework to net5.0. Additionally, I have a Directory.Build.props file with the following content:

This file links the common.props file to the .csproj files. After setting this up, I can use this variable in my project files and can update with it all my projects with one change in a single file. Update the TargetFramework of all your .csproj files with the following code:

After updating all project files, update all NuGet packages of your solution. You can do this by right-clicking your solution –> Manage NuGet Packages for Solution…

Update NuGet packages

Update your NuGet packages

That’s it. Your solution is updated to .NET 5.0. Build the solution to check that you have no errors.

Build the solution

Build the solution

Additionally, run all your tests to make sure your code still works.

Run all unit tests

Run all unit tests

Lastly, I update the path to the XML comments in the CustomerApi.csproj file with the following code:

Update CI pipeline

There are no changes required in the CI pipeline because the solution is built in Docker. Therefore, you have to update the Dockerfile. Replace the following two lines:

with

This tells Docker to use the new .NET 5.0 images to build and run the application. Additionally, you have to copy the previously created .props files into my Docker image with the following code inside the Dockerfile:

Check in your changes and the build pipeline in Azure DevOps should run successfully.

The .NET 5.0 build was successful

The .NET 5.0 build was successful

Conclusion

Today, I showed how easy it can be to upgrade .NET Core 3.1 to .NET 5.0. The upgrade was so easy because I kept my solution up to date and because microservices are small solutions that are way easier to upgrade than big monolithic applications. The whole upgrade for both my microservices took around 10 minutes. I know that a real-world microservice will have more code than mine but nevertheless, it is quite easy to update it. If you are coming from .NET Core 2.x or even .NET 4.x, the upgrade might be harder.

You can find the code of this demo on GitHub.

This post is part of “Microservice Series - From Zero to Hero”.

This post is licensed under CC BY 4.0 by the author.

Run xUnit Tests inside Docker during an Azure DevOps CI Build

C# 9.0 - What's new

Comments powered by Disqus.