Advertisement
Intermediate ASP.NET Core, Dependency Injection

How do you register multiple implementations of the same interface and resolve all of them together via IEnumerable<T>?

Short answer

Calling AddScoped/AddSingleton/AddTransient multiple times for the same interface with different implementations registers all of them; injecting IEnumerable resolves the full collection in registration order, useful for patterns like a chain of validators or multiple…

Save ASP.NET Core mock interview
Advertisement
AModel answer
Calling AddScoped/AddSingleton/AddTransient multiple times for the same interface with different implementations registers all of them; injecting IEnumerable<TInterface> resolves the full collection in registration order, useful for patterns like a chain of validators or multiple notification channels that should all run for a given event.
builder.Services.AddScoped<INotifier, EmailNotifier>();
builder.Services.AddScoped<INotifier, SmsNotifier>();
builder.Services.AddScoped<INotifier, PushNotifier>();

public class NotificationService(IEnumerable<INotifier> notifiers) {
    public async Task NotifyAllAsync(string message) {
        foreach (var notifier in notifiers) await notifier.SendAsync(message);
    }
}
Real-world example
An order confirmation feature registers Email, SMS, and Push notification implementations of INotifier, then injects IEnumerable<INotifier> to fan out a single order-confirmed event to every configured notification channel simultaneously.
Likely follow-ups
  • How do you resolve just the LAST registered implementation instead of all of them?
  • How do keyed services (added in .NET 8) provide a more explicit alternative to this pattern?
Related topics