title: How to register multiple implementations with its own interface in ASP.NET Core using Reflection?
body: <p>I'm new to the Dependency Injection in ASP.NET Core 3.1, and I'm trying to create Singleton instances of my Repository classes using Reflection, but I can't get it to work.</p>
  <p>Currently, the <code>BookService</code> uses the <code>BookRepository</code> from the DI:</p>
  <pre class="lang-cs prettyprint-override"><code>public class BookService : IBookService
  {
      private readonly IBookRepository _bookRepository;
  
      public BookService(IBookRepository bookRepository)
      {
          _bookRepository = bookRepository;
      }
  
      public async Task&lt;Book&gt; GetById(string id)
      {
          var find = await _bookRepository.FindAsync(id);
          return find;
      }
  }
  </code></pre>
  <p>It works because I added a singleton service to the container, with the following code (<a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicecollectionserviceextensions.addsingleton?view=dotnet-plat-ext-5.0#Microsoft_Extensions_DependencyInjection_ServiceCollectionServiceExtensions_AddSingleton__2_Microsoft_Extensions_DependencyInjection_IServiceCollection_" rel="nofollow noreferrer">link to Microsoft Docs</a>):
  <code>services.AddSingleton&lt;IBookRepository, BookRepository&gt;();</code></p>
  <p>I'm trying to achieve the same result using Reflection.</p>
  <p><code>BookRepository</code></p>
  <pre class="lang-cs prettyprint-override"><code>public class BookRepository : BaseRepository&lt;Book&gt;, IBookRepository
  {
  }
  </code></pre>
  <p><code>IBookRepository</code></p>
  <pre class="lang-cs prettyprint-override"><code>public interface IBookRepository : IAsyncRepository&lt;Book&gt;
  {
  }
  </code></pre>
  <p>This is what I have so far:</p>
  <pre><code>// Get all classes implementing IAsyncRepository
  var repositoryTypes = assembly.GetTypes()
      .Where(x =&gt; !x.IsInterface
          &amp;&amp; x.GetInterface(typeof(IAsyncRepository&lt;&gt;).Name) != null);
          
  foreach (var repositoryType in repositoryTypes)
  {
     // Adds a singleton service of BookRepository
     services.AddSingleton(repositoryType);
  }
  </code></pre>
  <p>But as you can see, the code above is adding only the <code>BookRepository</code>, missing to reference the <code>IBookRepository</code> interface, so it's throwing the following error:</p>
  <blockquote>
  <p>System.ArgumentException: 'Cannot instantiate implementation type 'IBookRepository' for service type 'IBookRepository'.'</p>
  </blockquote>
  <pre><code>
  Do you know how can I do that?
  
  **EDIT:** This is the implementation I made to solve the problem:
  
  ``` c#
  public static class DependencyInjectionExtensions
  {
      public static void AddApplicationServices(
          this IServiceCollection services)
      {
          var assembly = Assembly.GetExecutingAssembly();
  
          RegisterImplementationsOfServiceType(
              services, assembly, typeof(IService));
          RegisterImplementationsOfServiceType(
              services, assembly, typeof(IAsyncRepository&lt;&gt;));
      }
  
      private static void RegisterImplementationsOfServiceType(
          IServiceCollection services, Assembly assembly, Type type)
      {
          var implementationsType = assembly.GetTypes()
              .Where(x =&gt; !x.IsInterface &amp;&amp; x.GetInterface(type.Name) != null);
              
          foreach (var implementationType in implementationsType)
          {
              var servicesType = implementationType.GetInterfaces()
                  .Where(r =&gt; !r.Name.Contains(type.Name));
                  
              foreach (var serviceType in servicesType)
              {
                  services.AddSingleton(serviceType, implementationType);
              }
          }
      }
  }
  </code></pre>