SECRET OF CSS

Migrating ASP .NET Core 3.1 Web App to Core 6


.NET 6 unifies the .NET platform for the future. It also introduces 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, and hot-reload are just a few of the reasons you would want to transition to .NET 6 and ASP .NET Core 6.

You can check the official ASP.NET Core 6.0 documentation 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. In later posts, I’ll cover the new features of .NET 6.

Sample Application

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

Here is how the solution looks in Visual Studio.

Solution in Visual Studio

The source code of this application is available on this Git repo (master branch). You can also check out the deployed application.

Deployed application dashboard

This application also uses the 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.

You can use your own application code and the 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 installs/upgrades SDK).
  • Upgrade TargetFramework in our solution.
  • Update NuGet Packages.
  • Build and test.

In the 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:

Visual Studio Installer

Once Visual Studio is installed, you can use the following commands for some details:

Details of commands for Visual Studio

I also installed AWS Toolkit for Visual Studio. 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.

Install individual components

As you can see, that solution 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 the application code, but if you are interested in how the application is built, details can be found in the book “Build Accounting Application Using .NET Core, Angular and Entity-Framework.”

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

Application running as expected

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

Migrating AccountingBook.Core Project (Standard Library)

Let’s start with the AccountingBook.Core project, as it has no dependency on other projects, so it is the simplest to migrate.

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

AccountingBook.Core.csproj changes

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.

In the same process, let’s open up the .csproj file in visual studio for this project:

Update NuGet packages

So, we need to update the TargetFramework to net6.0, which we 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. The following picture shows packages being updated:

Update all packages

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 the .csproj file looks after the update:

.csproj file 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 are the .csproj files for the web project:

 .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.

Update all packages

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

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

Migrating AWS Serverless Lambda to .NET 6 Runtime

You can skip this section if you are not using AWS Serverless Lambda.

AWS covers .NET 6 upgrade details in this AWS Compute 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 Files

aws-lambda-tools-defaults.json file: no schema selected
aws-lambda-tools-defaults.json file: serverless.template

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

Publish AWS Serverless Application

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

Lambda function is updated to .NET6 runtime

Additionally, here we can see that the migrated application is up and running:
Migrated application is up and running

Migration Notes

As you may have noticed, during the migration of the 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 in 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 a newer .NET version is a straightforward process and it doesn’t require us to do many changes to our application.

We also saw that with simple changes, AWS serverless Lambda 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 refactor different areas of our application with new changes offered by the framework.

You can download the updated application code from the dotnet6 branch of the Git repository linked earlier in this article. 

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



News Credit

%d bloggers like this: