Tuesday, July 26, 2022

Modernize legacy .Net Framework WebApi apps with .Net Core ILogger & DI

This post is advised to read after the previous post about modernizing the .Net Framework console app to use .Net Core Programming model without changing the runtime. It helps to slowly migrate large monolith .Net Framework apps into .Net.

This post is about slowly migrating legacy WebAPI applications in the same way. ie adding DI using ServiceCollection and ILogger without changing the .Net Framework runtime.

Sample source code

This time there is no video tutorial. Let us look at the actual code itself.

Below goes the areas that need to be changed to get dependency injection from the Controller classes themselves.

Creating the ServiceCollection

This is obviously the first step in doing the DI.

Hope the code is explanatory if we have some basic idea about the .Net Core programming model. The Get() returns the ServiceProvider after configuring ServiceCollection.

Overriding DependencyResolver

It's easy to see the code first.

We just added our own DependencyResolver. Next, let us see what is in that new resolver class.

This facilitates the creation of objects as and when asked by the ASP.Net WebAPI framework.

ILogger Support

Nothing special to be done. The ConfigureServices() in ServiceProviderFactory class does it.

References


No comments: