Get all services injected as Keyed
I've injected some items using
AddKeyedScoped and a on service I need to get all implementations
In my mind, it should work
There are some how to get all services that implement IMyDependency?7 Replies
Have you figured out how?
Questions:
Why did you register the services as keyed?
Are you actually resolving keyed services?
Did you run this to check if it works (you didn't mention)?
Notes:
IIRC, calling
AddScoped is not idempotent and will actually register multiple implementations of IMyDependency, with the last one to be added being resolved when injecting IMyDependency, and all being resolved when injecting IEnumerable<IMyDependency>.Let me rephrase that better.
Contract and implementations
My goal: I want to perform parallel or sequential
Paralell triggers
Sequential triggers
@ste
I resolve this way: injecting MyImplementation twice, one using
Keyed and another not
I think it's bit weird.the DI works im pretty sure u r registering in total 4 scoped services here.
so for example if u want to only register 1 instance of
MyImplementationA as keyed service and generally for the interface, u would have to provide a factory delegate:
but from the way u describe ur problem, i wouldnt use keyed services at all.
i would simply register them as implementation type and as interface:
and then
would simply become
this might be off, because i dont really get what the IServiceBusClient is/doesServiceBusClient it's a message broker client, used to send items to queue
I get it. But it's not testable
I've tried to implement a factory
But the open-closed principle may kill me 🙂
Because, for each implementation I will need edit my DI config and
Factory classSolid isn't real, it cannot hurt you
u have to tell DI to use the factory to create the service
eg
.AddScoped<IMyDependency>(sp => sp.GetRequiredService<Factory>().Create())
DI wont magically use services that are registered just because their names include Factory
and as far as i know u cant create multiple services with one call
(not to mention that what ur factory does isnt even really doing anything u couldnt do by just injecting IEnumerable<IMyDependency>)