In Asp.Net Core, How Can Multiple Implementations Of The Same Interface Be Registered In C#?

To register multiple implementations of the same interface in ASP.NET Core, you can make use of the built-in dependency injection container. You need to configure the container to recognize and resolve multiple implementations for the interface.

Here’s an example of how you can register multiple implementations:

  1. First, define your interface, let’s say ISomeInterface:
public interface ISomeInterface
{
    void SomeMethod();
}
  1. Create the implementations of the ISomeInterface:
public class SomeImplementation1 : ISomeInterface
{
    public void SomeMethod()
    {
        Console.WriteLine("Implementation 1");
    }
}

public class SomeImplementation2 : ISomeInterface
{
    public void SomeMethod()
    {
        Console.WriteLine("Implementation 2");
    }
}
  1. In your Startup.cs class, within ConfigureServices method, register the implementations using the AddTransient or AddScoped method:
public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<ISomeInterface, SomeImplementation1>();
    services.AddTransient<ISomeInterface, SomeImplementation2>();
}

or

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<ISomeInterface, SomeImplementation1>();
    services.AddScoped<ISomeInterface, SomeImplementation2>();
}

Here, we have registered two implementations for the ISomeInterface using the AddTransient or AddScoped method.

Note that you may choose the appropriate lifetime option (AddTransient, AddScoped, or AddSingleton) based on your application’s requirements.

  1. Now, in your controller or service class, you can use the IEnumerable type to inject all the registered implementations of ISomeInterface:
public class MyController : Controller
{
    private readonly IEnumerable<ISomeInterface> _implementations;

    public MyController(IEnumerable<ISomeInterface> implementations)
    {
        _implementations = implementations;
    }

    //...
}

ASP.NET Core will automatically inject the collection of registered implementations into the controller or service class.

You can then loop through the _implementations enumerable and call the required methods as per your application logic.

Note: If you want to register a single implementation for an interface, use AddTransient, AddScoped, or AddSingleton methods without registering multiple implementations.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!