Upgrading Microservice .NET 5.0 to .NET 6.0
System Requirements for .NET 6.0
Follow the following steps:
- Check the breaking changes in .NET 6 to make sure that your code is compatible with the new version.
- Change the TargetFramework from net5.0 to net6.0 for every *.csproj file.
by right click on the project in VS2022 Properties and under Target Framework Dropdown, change from .NET 5 to .NET 6. After updating the .NET version. If you have a common.props file in the root folder of each microservice then change it in the changing the DefaultTargetFramework as following:
<PropertyGroup>
<DefaultTargetFramework>net5.0</DefaultTargetFramework>
<DefaultTargetFramework>net6.0</DefaultTargetFramework>
</PropertyGroup>
rebuild your solution and check that everything still builds. If you get an error, do a “clean solution” or delete all bin folders and try again.
3. Update your NuGet packages to the newest version (If it is needed).

Build the solution again and see if there is any error.
If you project using xUnit, you might have some errors since xUnit changedd Throw with ThrowAsync when you are checking for exceptions. Replace Throw with ThrowAsync wherever needed and rebuild the application.
4. Run all your unit tests and make sure that all tests still run successfully.

5. Update Dockerfiles
If your microservices run in Docker containers and therefore you have to update the Dockerfile to use the image for .NET 6.
Replace the following two lines:
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
With these:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
If your project uses Docker also to build the application in the CI pipeline. You don’t have to change anything in your build pipeline.
6. Update Swagger and Swagger UI
Remove the Swagger comment configuration in the .csproj file. This is not needed anymore.
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
DocumentationFile>obj\Debug\net5.0\CustomerApi.xml</DocumentationFile> <NoWarn>1701;1702;1591</NoWarn> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> <DocumentationFile>obj\Release\net5.0\CustomerApi.xml</DocumentationFile> <NoWarn>1701;1702;1591</NoWarn> </PropertyGroup>
7.update the service definition in the Startup.cs class.
You need in ConfigureServices() method, AddEndpointsApiExplorer and AddSwaggerGen. You can add additional information like an email and description to AddSwaggerGen.
services.AddEndpointsApiExplorer();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Product Api",
Description = "A simple API to create a product",
Contact = new OpenApiContact
{
Name = "Mehrdad Zandi",
Email = "mehrdad.zandi@softsolutionsahand.com",
Url = new Uri("http://www.softsolutionsahand.com/")
}
}
});
});
In the last add UseSwagger and UseSwaggerUI to the Configure() method.
pp.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;
});
he RoutePrefix property allows you to configure the route to the Swagger UI. Setting it to string.Empty configures the application to display the Swagger UI when no URL is entered.
In case you don’t want to update the configuration and use Swagger as always, all you have to do is update the XML comment section in the .csproj from .net5.0 to .net6.0.
8. Run the updated CI Pipeline
Check in your changes and the build pipeline in Azure DevOps should run successfully.


new .NET 6 Features
.NET 6 put an emphasis on simplifying the structure of the project and files. You can change your files to use these features like global usings or top-level statements. You can also choose to keep the structure the way it already is. Both work just fine.
Conclusion
Upgrading from .NET 5 to .NET 6 is very fast and simple. Most applications should be able to update without any problems by simply changing the .NET version number in the project file and the Dockerfile. Make sure to update your NuGet packages and test your application after the update to make sure that everything still works.
Upgraded code can be found on GitHub.
This post is part of “Microservices-Step by step”.