SHARE:

My migration experience from ASP.NET Core 2.2 to 3.1, part 2: Changes in Program.cs

My personal experience in practice

My personal experience did not particularly traumatize me, on the contrary, it was rather simple.
If you have been following me for a long time, you will recognize the examples that I have been using for a while, my GitHub repository implementing the essential features of an ASP.NET Core WebAPI.

Changes in Program.cs

Even if it’s not new thing, (because it’s already existing in ASP.NET Core 2.1 and 2.2), ASP.NET Core 3.0 and 3.1 applications build and run by default a IHost interface from a IHostBuilder instead of building and running a IWebHost from a IWebHostBuilder.

The difference is mainly on the performance, better with IHost.

IHost belongs to the package Microsoft.Extensions.Hosting while IWebHostBuilder belongs to Microsoft.AspNetCore.Hosting package.

IWebHostBuilder is still accessible for configuring your WebAPI by using the method ConfigureWebHostDefaults. So it causes changes to the program.cs structure, but not that much.

UseKestrel method is now required to specify which server type is used for the application, and it has to be added on the IWebHostBuilder parameter in ConfigureWebHostDefaults method, same for UseSerilog method and UseStartup method.

ConfigureAppConfiguration method doesn’t change at all.

Because I use Serilog, I needed to add ConfigureLogging method on my IHostBuilder and add Serilog on, before I was able to configure Serilog in Startup.cs by configuring ILoggerFactory by adding Serilog on, but this factory is no longer available in Startup.cs with ASP.NET Core 3+.

Here is what my Program.cs looks after my migration:

And before (ASP.NET Core 2.2)

Conclusion

It was not a real big deal to update my Program.cs. To be honest that change is not mandatory (IWebHostBuilder to IHostBuilder) but above all I wanted to reduce the legacy code by following Microsoft’s recommendations.

Written by

anthonygiretti

Anthony is a specialist in Web technologies (14 years of experience), in particular Microsoft .NET and learns the Cloud Azure platform. He has received twice the Microsoft MVP award and he is also certified Microsoft MCSD and Azure Fundamentals.