Skip to main content

Software Development

DI-Y: Crafting Code with Multiple Services Using IEnumerable in Dependency Injection

Istock 1445557229

Registering Services for IEnumerable Injection

Service Registration Fundamentals

Service registration is the process of telling the DI container how to create instances of a service, typically done at application startup.

Registering Services for IEnumerable

When services implement a common interface, you can register them in such a way that the DI container can provide an IEnumerable of these services. In .NET, for example:

services.AddTransient<ICommonService, ServiceA>();
services.AddTransient<ICommonService, ServiceB>();
Why would you register mulitple instances of the same interface? One use-case is that it is the same service with a different configuration. Here is an example of how that would look:
services.Configure<CustomOption>("Opt1", configuration.GetSection("Options1"));
services.Configure<CustomOption>("Opt2", configuration.GetSection("Options2"));

builder.Services.AddSingleton<ICommonService,ServiceA>(sp => { 
    var options = sp.GetRequiredService<IOptionsMonitor<CustomOption>>().Get("Opt1"); 
    return new ServiceA(options);
});

builder.Services.AddSingleton<ICommonService, ServiceA>(sp => {
    var options = sp.GetRequiredService<IOptionsMonitor<CustomOption>>().Get("Opt2");
    return new ServiceA(options);
});

Best Practices in Registration

Maintain logical organization in your service registrations. Grouping related services and using extension methods can lead to more maintainable code.

Resolving Services as IEnumerable

Injecting and Using IEnumerable of Services

When you have multiple services registered under the same interface, you need to inject an IEnumerable of a service interface, the DI container provides all registered implementations of that interface. For example:

public class ConsumerClass
{
    private readonly IEnumerable<ICommonService> _services;

    public ConsumerClass(IEnumerable<ICommonService> services)
    {
        _services = services;
    }

    public void UseServices()
    {
        foreach (var service in _services)
        {
            service.PerformAction();
        }
    }
}
You can also pluck a service out of the IEnumerable with a Where clause, or add your own resolver:
public class ConsumerClass
{
    private readonly ICommonService _myService;

    public ConsumerClass(IEnumerable<ICommonService> services)
    {
        _services = services.Where(x => x.Name = "Mine");
    }

    public void UseServices()
    {
        foreach (var service in _services)
        {
            service.PerformAction();
        }
    }
}

Managing Multiple Implementations

When injecting an IEnumerable of services, it’s crucial to handle each service appropriately, leveraging their individual functionalities.

Advanced Topics and Pitfalls

Understanding Service Lifecycles

Recognize the lifecycles of your services.

  • Transient: New instance with every object that needs it
  • Scoped: New instance for the application request, dependent object will share the instance until the request is completed
  • Singleton: New instance for the lifetime of the application. These services will only create new instances when the application is restarted.

Avoiding Common Errors

Be cautious of using the service locator pattern and ensure that DI is not leading to tightly coupled code.


Remember, effective use of DI, especially with IEnumerable injections, enhances the flexibility and maintainability of your code. Keep experimenting and refining your approach for the best results. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Greg Jeffers, Senior Technical Consultant

Greg Jeffers is a Senior Technical Consultant with Perficient. He has been developing software on the Microsoft stack for 20+ years and working with Optimizely for 5. Having been in several roles across multiple industries, Greg brings a holistic approach to development. He is passionate about finding the right balance of people and processes to make users feel comfortable in the application while being performant.

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram