octocat.dev.

A Developer's blog.About

Dependency Injection – Registering Generic Types in ASP.NET Core

Shubhan Chemburkar
Shubhan Chemburkar
This post is cross post from archived blog at worldwidecode.wordpress.com

Source: Steve Gordon’s blog post

We want to be able to ask for an IThing<SomeType> in the constructor of a consumer which will get the correct GenericThing<SomeType> injected. We can use a extension method on the ServiceCollection that accepts the types as parameters. Our registration would then look like this

serviceCollection.AddSingleton(typeof(IThing<>), typeof(GenericThing<>));

Related Articles

The source code for this blog is available on GitHub.