Register Generic Classes in ASP.NET Core DI
Recently, I worked on a very simple and straight requirement where I had to provide different implementation to an interface but to my surprise, it wasn’t that easy when I had to work with generics.
The Requirement
The requirement was to provide two different implementations to a generic interface. One concrete class should pass ‘string’ and another concrete class should pass ‘byte[]’ as its type parameter and register the classes in the service container of the ASP.NET Core, so that other classes can demand the instances via built-in IoC container.
The Solution
It turned out that when we register our generic classes, we need to specify the type parameter as well.
Now, wherever we need these classes we just need to ask for it in our dependent classes.