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

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

My feeling about this step

Once again it was rather simple, but a little harder than what I made Program.cs, I needed to rewrite some bigger pieces of code, but not that big.
Like the previous article, my examples are based on my GitHub repository implementing the essential features of an ASP.NET Core WebAPI.

Changes in Startup.cs

ApplicationInsights configuration has been moved from Program.cs to Startup.cs, now you need to set AddApplicationInsightsTelemetry methods on services.

AddMvc method has been replaced by AddControllers (specific to WebAPI)

UseAuthorization method has been added, if you are using Authorization you must add that middleware to make your Authorization process work.

UseMvc method has been replaced by UseRouting method and UseEndpoints method. Note that these two methods must not be nested if you are using Authentication and Authorization. UseRouting method must be set before UseAuthentication and UseAuthorization methods, UseEndpoints method has to be set at the end.

IHostingEnvironment has been replaced by IHostEnvironment

ILoggerFactory and IServiceProvider are no longer available by injection dependency, injecting them in Startup constructor is no longer possible

If you were using them in Startup.cs (and elsewhere in the app) you will have to rewrite pieces of code that use them.

Examples of rewritten HttpExtension for Polly:

With ASP.NET Core 2.2:

With ASP.NET Core 3.1:

As I said before ASP.NET Core 3+ doesn’t provide anymore ILoggerFactory by injection dependency so I wasn’t able to inject it in parameter of my extension method. I had to use an overload of AddPolicyHandler method that uses a delegate taking in parameter IServiceProvider (available at this point) and I was able to create an instance of a ILogger to be sent to my Polly policies. It was not a big challenge but I needed to redesign my code.

NewtonSoft.Json has been replaced by System.Text.Json by default. So if you still want (you should not, but I just want to show you how to) NewtonSoft.Json you will have to configure it explicitely with AddNewtonsoftJson method and set it on AddControllers method.

What does the Startup would look like with ASP.NET Core 2.2 ?

What does the Startup would look like with ASP.NET Core 3.1 ?

Conclusion

At this step I needed to work a bit more, especially where I used ILoggerFactory and IServiceProvider by injection dependency.

A lot of stuff has not changed at all, like:

  • Authentication
  • Authorization (you have to add UseAuthorization method)
  • Validation
  • Caching
  • Compression
  • Versionning and more….

If you want to see my full configuration, you can check here:

Master branch (ASP.NET Core 2.2): https://github.com/AnthonyGiretti/commonfeatures-webapi-aspnetcore/blob/master/WebApiDemo/Startup.cs

Migration-to-aspnetcore3 branch (ASP.NET Core 3.1): https://github.com/AnthonyGiretti/commonfeatures-webapi-aspnetcore/blob/migration-to-aspnetcore3/WebApiDemo/Startup.cs