Fix Ambiguous Constructors in ASP.NET Dependency Injection!
Ambiguous ASP.NET Core dependency injection constructors can be resolved with an explicit constructor attribute, a factory, or keyed services.
Transcript 1 topic
Resolve ambiguous constructors
0:00If you're using ASP dot NET Core's dependency injection and you have a class with ambiguous constructors, things are not gonna go well. But there's three ways you can solve that. Number one, use the activator utilities constructor to choose the winner explicitly. Apply it to the constructor that you wanna use and then just register that service normally. Another way to do this is with a factory.
Simply choose the constructor you want when you're generating that service to be served up. And when you have multiple implementations, you can use keyed services to pull the one out of the dependency injection that you need. Just register your service as a keyed scoped or keyed singleton or whatever you need and give it a name. Then in the class that needs to receive that, use the attribute from keyed services. That'll give you the specific instance of the service that you registered to begin with.
Clear intent beats dependency injection guesswork any day of the week. Market, factory it, or key it.
Sign in to join in. Reading needs nothing.