- My migration experience from ASP.NET Core 2.2 to 3.1, part 1: Introduction
- My migration experience from ASP.NET Core 2.2 to 3.1, part 2: Changes in Program.cs
- 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 4: Changes in the rest of the application
Changes in the rest of the application
Again in this step, I haven’t encountered any big issues. Finally I had to refactor two services, and rewrite an optional one.
As I said in the previous steps ILoggerFactory and IServiceProvider are no longer available by injection dependency, then like in Startup.cs I needed to review their use. The optional one is the new JSON serializer with .NET Core 3.
.NET Core 3 introduces a new serializer
Json.Net (NewtonSoft) has been for a long time the most used JSON serializer in .NET world. Since .NET Core 3, Microsoft introduced a new one named System.Text.Json. Because it provides better performance I decided to rewrite my JSON serializer service.
My JSON serializer service with Json.Net (NewtonSoft):
My JSON serializer service with System.Text.Json:
If you want to do the same you can download the package here and rewrite our serializations like I did:
If you want to learn more about it, you can red this nice blog article here: https://www.c-sharpcorner.com/article/the-new-json-serializer-in-net-core-3/
Because ILoggerFactory is no longer available I had to rewrite a service.. This service has three behaviors (uses three classes depending the implementation I want to use) and I used to create three loggers (for each class) and inject each logger to its respective class.
This service is a reporting service generator that selects the appropriate class to generate the report (PDF, CSV, Word) Let’s see how my service looked like:
After removing ILoggerFactory and creating many loggers as necessary here is my new service:
Same job for IServiceProvider here. I have built a service that provides a specific instance of a service, obtained from an interface and a named instance. This use case that I have already blogged about is decribed here, I used it for dynamic injection dependency in a multitenancy scenario. I replaced IServiceProvider by IHttpContextAccessor, and I haven’t waited for ASP.NET Core 3+ because using IServiceProvider is a bad practice. IHttpContextAccessor is available in ASP.NET Core 2+. But with ASP.NET Core 3+, no choice, you have to IHttpContextAccessor for that kind of dependency resolution.
My service with IServiceProvider:
My service reimplemented with IHttpContextAccessor:
This article concludes my migration to ASP.NET Core 3.1.
That migration was fun and very instructive, I learned new things, and how bad using IServiceProvider was, that’s why Microsoft removed it in the dependency injection system.
This is was my migration experience only, I did not cover everything, if you want to learn more about migration for ASP.NET Core 2.2 to 3.0 you can read this article here and from ASP.NET Core 3.0 to 3.1 you can read this other article here.
Hope this serie of articles helped you 🙂