title: How to register multiple implementations of the same interface in Asp.Net Core?
body: <p>I have services that are derived from the same interface.</p>
  
  <pre><code>public interface IService { }
  public class ServiceA : IService { }
  public class ServiceB : IService { } 
  public class ServiceC : IService { }
  </code></pre>
  
  <p>Typically, other IoC containers like <code>Unity</code> allow you to register concrete implementations by some <code>Key</code> that distinguishes them.</p>
  
  <p>In ASP.NET Core, how do I register these services and resolve them at runtime based on some key?   </p>
  
  <p>I don't see any <code>Add</code> Service methods that take a <code>key</code> or <code>name</code> parameter, which would typically be used to distinguish the concrete implementation.</p>
  
  <pre><code>    public void ConfigureServices(IServiceCollection services)
      {            
           // How do I register services of the same interface?            
      }
  
  
      public MyController:Controller
      {
         public void DoSomething(string key)
         { 
            // How do I resolve the service by key?
         }
      }
  </code></pre>
  
  <p>Is the Factory pattern the only option here?</p>
  
  <p><strong>Update1</strong><br>
  I have gone though the article <a href="https://stackoverflow.com/questions/39072001/dependency-injection-resolving-by-name">here</a> that shows how to use the factory pattern to get service instances when we have multiple concrete implementations. However, it is still not a complete solution.  When I call the <code>_serviceProvider.GetService()</code> method, I cannot inject data into the constructor. </p>
  
  <p>For example consider this:</p>
  
  <pre><code>public class ServiceA : IService
  {
       private string _efConnectionString;
       ServiceA(string efconnectionString)
       {
         _efConnecttionString = efConnectionString;
       } 
  }
  
  public class ServiceB : IService
  {    
     private string _mongoConnectionString;
     public ServiceB(string mongoConnectionString)
     {
        _mongoConnectionString = mongoConnectionString;
     }
  }
  
  public class ServiceC : IService
  {    
      private string _someOtherConnectionString
      public ServiceC(string someOtherConnectionString)
      {
        _someOtherConnectionString = someOtherConnectionString;
      }
  }
  </code></pre>
  
  <p>How can <code>_serviceProvider.GetService()</code> inject the appropriate connection string?
  In Unity, or any other IoC library, we can do that at type registration. I can use <a href="https://docs.asp.net/en/latest/fundamentals/configuration.html#options-config-objects" rel="noreferrer">IOption</a>, however, that will require me to inject all settings.  I cannot inject a particular connection string into the service.</p>
  
  <p>Also note that I am trying to avoid using other containers (including Unity) because then I have to register everything else (e.g., Controllers) with the new container as well.</p>
  
  <p>Also, using the factory pattern to create service instances is against DIP, as it increases the number of dependencies a client has <a href="https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=100" rel="noreferrer">details here</a>.</p>
  
  <p>So, I think the default DI in ASP.NET Core is missing two things:</p>
  
  <ol>
  <li>The ability to register instances using a key</li>
  <li>The ability to inject static data into constructors during registration</li>
  </ol>