How to migrate ASP.NET Core 5 code to ASP.NET Core 6 | InfoWorld

2022-07-07 14:16:34 By : Ms. Hui Ding

By Joydip Kanjilal , Columnist, InfoWorld |

Microsoft’s ASP.NET Core 6, which has been available for production use since November 8, introduces a simplified hosting model that reduces the boilerplate code that you would otherwise need to write to get your ASP.NET Core application up and running. ASP.NET Core 6 makes a bit easier to create a new web application from scratch, compared with ASP.NET Core 5.

But what if you want to update an ASP.NET Core 5 project to ASP.NET Core 6? In that case, you should be aware of the code you will need to write to migrate ASP.NET Core 5 code to ASP.NET Core 6. This article presents several code samples that show how you can do this.

To work with the code examples provided in this article, you should have Visual Studio 2022 installed in your system. If you don’t already have a copy, you can download Visual Studio 2022 here.

First off, let’s create an ASP.NET Core project in Visual Studio 2022. Following these steps will create a new ASP.NET Core Web API 6 project in Visual Studio 2022:

We’ll use this ASP.NET Core 6 Web API project to illustrate migrations of ASP.NET Core 5 code to ASP.NET Core 6 in the subsequent sections of this article.

The following code snippet illustrates what a typical Program class looks like in ASP.NET Core 5.

With the introduction of the simplified hosting model in ASP.NET Core 6, you no longer have to use the Startup class. You can read more about this in my earlier article here. Here’s how you would write a typical Program class in ASP.NET Core 6:

The following code snippet shows how you can add a middleware component in ASP.NET Core 5. In our example, we’ll add the response compression middleware.

To add a middleware component in ASP.NET Core 6, you can use the following code.

To add an endpoint in ASP.NET Core 5, you can use the following code.

You can add an endpoint in ASP.NET Core 6 using the following code.

Note that in ASP.NET Core 6 you can add endpoints to WebApplication without having to make explicit calls to the UseRouting or UseEndpoints extension methods.

The following code snippet illustrates how you can add services to the container in ASP.NET Core 5.

To add services to the container in ASP.NET Core 6, you can use the following code.

You can test an ASP.NET Core 5 application using either TestServer or WebApplicationFactory. To test using TestServer in ASP.NET Core 5, you can use the following code snippet.

The following code snippet shows how you can test your ASP.NET Core 5 application using WebApplicationFactory.

You can use the same code to test using TestServer or WebApplicationFactory in .NET 5 and .NET 6. 

Logging providers in ASP.NET Core are used to store logs. The default logging providers included in ASP.NET Core are the Debug, Console, EventLog, and EventSource logging providers.

You can use the ClearProviders method to clear all logging providers and add a specific logging provider or your own custom logging provider. The following code snippet illustrates how you can remove all ILoggerProvider instances and add the Console logging provider in ASP.NET Core 5.

In ASP.NET Core 6, when you call WebApplication.CreateBuilder, it adds the Console, Debug, EventLog, and EventSource logging providers. The following code snippet shows how you can clear the default logging providers and add only the Console logging provider in ASP.NET Core 6.

The code examples provided here illustrate the different ways we add middleware, routing, services, and logging providers in ASP.NET Core 5 and in ASP.NET Core 6, as well as differences in the Program class and testing. These snippets should help you when working with ASP.NET Core 6 applications, and get you off to a good start when you migrate your ASP.NET Core 5 applications to ASP.NET Core 6.

Joydip Kanjilal is a Microsoft MVP in ASP.Net, as well as a speaker and author of several books and articles. He has more than 20 years of experience in IT including more than 16 years in Microsoft .Net and related technologies.

Copyright © 2022 IDG Communications, Inc.

Copyright © 2022 IDG Communications, Inc.