title: No service for type 'MyType' has been registered
body: <p>I have a generic repository architecture that looks like this:</p>
  
  <p><strong>Repository</strong></p>
  
  <pre><code>public interface IRepository&lt;T&gt; where T: class
  {
      IList&lt;T&gt; Get(Func&lt;T, bool&gt; where);
  }
  
  public abstract class Repository&lt;T&gt; : IRepository&lt;T&gt; where T: class
  {
      private readonly DbSet&lt;T&gt; _entity;
  
      protected Repository(ApplicationDbContext dbContext)
      {
          _entity = dbContext.Set&lt;T&gt;();
      }
  
      public IList&lt;T&gt; Get(Func&lt;T, bool&gt; where)
      {
          return _entity.Where(where).ToList();
      }
  }
  </code></pre>
  
  <p>Then concrete implementations are created like this:</p>
  
  <p><strong>UserRepository</strong></p>
  
  <pre><code>public class UserRepository : Repository&lt;ApplicationUser&gt;
  {
      public UserRepository(ApplicationDbContext dbContext) : base(dbContext) {}
  
      // Can add any other methods I want that aren't included in IRepository
  }
  </code></pre>
  
  <p>I'll likely have quite a few services, so rather than having each one passed into the controller individually, I thought I'd try passing in a single factory that can produce repositories for me.</p>
  
  <p><strong>RepositoryFactory</strong></p>
  
  <pre><code>public interface IRepositoryFactory
  {
      T GetRepository&lt;T&gt;() where T : class;
  }
  
  public class RepositoryFactory : IRepositoryFactory
  {
      private readonly IServiceProvider _provider;
  
      public RepositoryFactory(IServiceProvider serviceProvider)
      {
          _provider = serviceProvider;
      }
  
      public T GetRepository&lt;T&gt;() where T : class
      {
          return _provider.GetRequiredService&lt;T&gt;(); // ERROR: No service for type 'UserRepository' has been registered
      }
  }
  </code></pre>
  
  <p>Now, in setting up dependency injection, I registered the services like this:</p>
  
  <p><strong>Startup.cs</strong></p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      // [...]
  
      services.AddScoped&lt;IRepository&lt;ApplicationUser&gt;, UserRepository&gt;();
      services.AddScoped&lt;IRepositoryFactory, RepositoryFactory&gt;();
  
      // [...]
  }
  </code></pre>
  
  <p>This is all used in the controller like this:</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public class HomeController : Controller
  {
      private readonly UserRepository _userRepository;
  
      public HomeController(IRepositoryFactory repositoryFactory)
      {
          _userRepository = repositoryFactory.GetRepository&lt;UserRepository&gt;(); // ERROR: No service for type 'UserRepository' has been registered
      }
  
      // [...]
  }
  </code></pre>
  
  <p>When I call <code>_provider.GetRequiredService&lt;T&gt;()</code> in the <code>repositoryFactory.GetRepository&lt;UserRepository&gt;()</code> method, I get the error in the comment above.</p>
  
  <p>The <code>RepositoryFactory</code> is coming through just fine, but the <code>UserRepository</code> isn't getting registered. What am I missing? I've tried calling the <code>GetRepository</code> method outside of the constructor, and I've tried change <code>AddScoped</code> to the other <code>Add</code> variants (Transient and Singleton), but to no avail.</p>