Migrating ASP .NET Core 3.1 Web Application to ASP .NET Core 6

Introduction

.NET 6 unifies the .NET platform for future. It also introduced plenty of exciting new features along the way. It has achieved the ONE .NET Vision of Microsoft and also opens up new horizons for existing .NET developers. Improved performance, C# 10 features, hot-reload etc. are few of the reasons you would want to transition to .NET 6 and ASP .NET Core 6.

You can check the official website for more details about what’s new in ASP .NET Core 6.

Our focus in this post is to see a simple process of migrating from ASP .NET Core 3 to ASP .NET 6 and in later posts, I’ll cover the new features of .NET 6 as well.

Sample Application

I’ll be using an existing web application which is developed using ASP .NET Core 3.1. It has a very common web application structure and is written around accounting domain and have typical UI (Angular), API (ASP .NET Core 3.1) and uses Postgres for Data Layer.

Here is how solution looks in visual studio.

The source code of this application is available on this git repo (master branch).

You can also check the deployed application on this URL.

This application also uses AWS Serverless template (it can be deployed as a serverless Lambda function on AWS). If you are not using AWS Serverless, you can simply ignore this detail.

Now, you can use your own application code and principles will be the same.

In this post, we will be upgrading the backend of our solution to .NET 6.

Migration Steps

Here are the steps which we will take for the migration process:

  • Install Visual Studio 2022 (this also install/upgrade SDK).
  • Upgrade TargetFramework in our solution.
  • Update NuGet Packages.
  • Build and Test.

In next sections, we will see all of these steps to migrate our solution to .NET 6.

Installing .NET 6

You can download and install standalone .NET SDK or you can just download and install visual studio 2022 and it will automatically install .NET 6 as part of its installation. Also select Web workload during installation.

Following screenshot shows the installation of Visual Studio 2022 community edition:

Once visual studio is installed, you can use following commands for some details:

I also installed AWS Toolkit for Visual Studio from this link. Again, you can skip this if not using AWS for .NET.

Open .NET Core Solution in Visual Studio

Once visual studio 2022 installation is done and .NET 6 is installed. I opened the ASP .NET Core 3 solution in visual studio 2022 and encountered the following notice, click Install and it will install the components required.

As you can see that solutions contains different projects arranged in layers for different application concerns:

  • AccountingBook.Core (standard library, domain model)
  • AccountingBook.Data(standard library, entity framework data-layer)
  • AccountingBook.Web(ASP .NET Core 3.1 web api)

I will not go into the details of application code but if you are interested in how the application is built, you can check this link for details.

Now, if we want, we can run our application and the following picture shows that it is working as expected:

Next, we will migrate one by one all projects in solution to .NET 6.

Migrating AccountingBook.Core Project (Standard Library)

Lets start with AccountingBook.Core project as it has no dependency on other projects so it is simplest to migrate.

Open .csproj file of the project and change it to follows:

That’s all we needed to do to migrate the library project. We can test by building this project in visual studio.

Migrating AccountingBook.Data Project (Standard Library)

This is also a standard library project but it references AccountingBook.Core project (Which we already migrated to net6.0) and it also has some nuget packages including Entity Framework Core.

The same process, lets open up the .csproj file in visual studio for this project:

so, we need to update the TargetFramework to net6.0 which can do simply by editing the file directly as we did with the previous project.

However, to update nuget packages, I liked to use visual studio’s package-manager window for the project, as it provides more details. Following picture shows packages being updated:

With these changes (TargetFramework and nuget packages update), migration is done for this project. We can test by building this project in visual studio.

Here is how .csproj file looks after the update:

Migrating AccountingBook.Web Project (Web API)

This project references both AccountingBook.Core and AccoutingBook.Web projects along with its own packages. However, the process is almost identical to what we did in previous sections.

Here is the .csproj files for the web project:

Following the same process, we will first upgrade the TargetFramework to net6.0 and then use package-manager window to update packages.

Here is how the .csproj file looks after the updates:

We can now build and run the solution to check that is working as expected after the upgrade.

Migrating AWS Serverless Lambda to .NET 6 Runtime

Now, you can skip this section if you are not using AWS Serverless lambda.

AWS covers .NET 6 upgrade details in this blog post along with the steps required to migrate as shown. Following is what we need to do for the migration of .NET Lambda functions to .NET 6 runtime:

  1. Open the aws-lambda-tools-defaults.json file, if it exists:
    1. Set the function-runtime field to dotnet6
    2. Set the framework field to net6.0. If you remove the field, the value is inferred from the project file.
  2. If it exists, open the serverless.template file. For any AWS::Lambda::Function or AWS::Servereless::Function resource, set the Runtime property to dotnet6

aws-lambda-tools-defaults.json file

aws-lambda-tools-defaults.json file

With these changes done, we can build the project locally and once verified, solution can be deployed AWS Serverless Lambda:

once deployed, we can see from the AWS web console that lambda function is updated to .NET6 runtime as well:

and here we can see that migrated application is up and running:

Migration Notes

As you may have noticed, that during the migration of solution, I did not change any of the application code and still was able to migrate the solution with ease. This is due to the fact that most structural changes in .NET 6 are optional, so you can decide later one by one which areas you want to change.

Even without doing any structural changes, our migrated application will still benefit from the platform upgrade such as performance

Summary

.NET 6 is the way forward for solutions built using .NET technology stacks. In this post, we saw that migrating an ASP .NET Core 3.1 Web API project to newer .NET version is straight forward process and it doesn’t require us to do much changes to our application.

We also saw that with simple changes, AWS serverless Lambda part was easy to upgrade to .NET6 runtime as well.

Even without doing any code changes to our application, the updated solution will have performance benefits and later as we progress, we can can refactor different areas of our application with new changes offered by the framework.

You can download the updated application code from this git repository (dotnet6 branch).

Let me know if you have some comments or questions. Till next time, Happy Coding.