C
C#2mo ago
Thalnos

Inject multiple implementations of the same interface

what's the proper way to inject multiple implementations of the same interface?
public class FooBar{
public FooBar(IInterface something){

}
}
builder.Services.AddScoped<IInterface, Implementation1>();
builder.Services.AddScoped<IInterface, Implementation2>();
public class FooBar{
public FooBar(IInterface something){

}
}
builder.Services.AddScoped<IInterface, Implementation1>();
builder.Services.AddScoped<IInterface, Implementation2>();
is a setup like this possible? Which one is being injected? How to have different classes use different impelmentations without them using concrete dependencies of Implementation1 and Implementation2?
3 Replies
Thalnos
ThalnosOP2mo ago
I see thank you "and adds to the previous one when multiple services are resolved via IEnumerable<IMyDependency>" Does that mean when my ctor takes a List<IInterface> then [implementation1, implementation2] is being injected or how does that work? got it thx /close $close
MODiX
MODiX2mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Thalnos
ThalnosOP2mo ago
can't mark the thread as answered somehow

Did you find this page helpful?