title: Unable to resolve dependency for generic repository using autofac
body: <p>I am working with .net core API and resolving my dependency using autofac. However somehow I am not able to resolve dependency for my Generic Repository. </p>
  
  <p>Can someone guide what I have done wrong. </p>
  
  <p><strong>startup.cs</strong></p>
  
  <pre><code>var builder = new ContainerBuilder();
  
  builder.RegisterType&lt;UnitOfWork&gt;().As&lt;IUnitOfWork&gt;()
      .InstancePerLifetimeScope();
  
  builder.RegisterType&lt;DatabaseFactory&gt;().As&lt;IDatabaseFactory&gt;()
  .InstancePerLifetimeScope();
  
  //This is the place which is not working for me
  builder.RegisterGeneric(typeof(Repository&lt;&gt;)).As(typeof(IRepository&lt;&gt;)).InstancePerLifetimeScope();
  
  builder.RegisterAssemblyTypes(typeof(SizeRepository).Assembly)
      .Where(t =&gt; t.Name.EndsWith("Repository"))
      .AsImplementedInterfaces()
      .InstancePerLifetimeScope();
  
  builder.RegisterAssemblyTypes(typeof(UserService).Assembly)
      .Where(t =&gt; t.Name.EndsWith("Service"))
      .AsImplementedInterfaces()
      .InstancePerLifetimeScope();
  
  builder.Populate(services);
  var container = builder.Build();
  //Create the IServiceProvider based on the container.
  return new AutofacServiceProvider(container);
  </code></pre>
  
  <p><strong>UnitOfWork.cs</strong> </p>
  
  <pre><code>using System.Threading.Tasks;
  
  namespace MakeTalk.Data.Infrastructure
  {
  public class UnitOfWork : IUnitOfWork
  {
      #region Local variable
  
      private readonly IDatabaseFactory databaseFactory;
      private MakeTalkEntities dataContext;
  
      #endregion
  
      #region Constructor
      public UnitOfWork(IDatabaseFactory databaseFactory)
      {
          this.databaseFactory = databaseFactory;
      }
      #endregion
  
      #region Property
      protected MakeTalkEntities DataContext =&gt; dataContext ?? (dataContext = databaseFactory.Get());
      #endregion
  
      #region Methods
      /// &lt;summary&gt;
      /// Commit changes 
      /// &lt;/summary&gt;
      /// &lt;history&gt;Created : 01-04-2018&lt;/history&gt;
      public void Commit()
      {
          DataContext.SaveChanges();
      }
      /// &lt;summary&gt;
      /// Commit Async changes
      /// &lt;/summary&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      /// &lt;history&gt;Created : 01-04-2018&lt;/history&gt;
      public async Task&lt;int&gt; CommitAsync()
      {
          return await DataContext.SaveChangesAsync();
      }
  
      #endregion
  
  }
  }
  </code></pre>
  
  <p><strong>Repository.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Linq.Expressions;
  using System.Threading.Tasks;
  using Microsoft.EntityFrameworkCore;
  
  
  namespace MakeTalk.Data.Infrastructure
  {
      public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class
      {
          #region Variable / Properties  
          private MakeTalkEntities dataContext;
          private readonly DbSet&lt;T&gt; dbSet;
          protected MakeTalkEntities DataContext =&gt; dataContext ?? (dataContext = DatabaseFactory.Get());
      protected IDatabaseFactory DatabaseFactory
      {
          get;
          private set;
      }
      #endregion
  
      #region Constructor
      protected Repository(IDatabaseFactory databaseFactory)
      {
          DatabaseFactory = databaseFactory;
          dbSet = DataContext.Set&lt;T&gt;();
      }
      #endregion
  
      #region Sync Methods
  
      // all methods ...
      #endregion
  }
  }
  </code></pre>
  
  <p><strong>DatabaseFactory.cs</strong></p>
  
  <pre><code>using Microsoft.EntityFrameworkCore;
  
  namespace MakeTalk.Data.Infrastructure
  {
      public class DatabaseFactory : Disposable, IDatabaseFactory
      {
          private MakeTalkEntities dataContext;
  
          public MakeTalkEntities Get()
          {
              return dataContext ?? (dataContext = new MakeTalkEntities(new DbContextOptions&lt;MakeTalkEntities&gt;()));
          }
          protected override void DisposeCore()
          {
              dataContext?.Dispose();
          }
      }
  }
  </code></pre>
  
  <p><strong>MakeTalkEntities.cs</strong></p>
  
  <pre><code>using Microsoft.EntityFrameworkCore;
  using MakeTalk.Data.Configuration;
  using MakeTalk.Model;
  
  namespace MakeTalk.Data
  {
      public class MakeTalkEntities : DbContext
      {
          #region Constructor
  
          public MakeTalkEntities(DbContextOptions options)
          {
  
          }
  
          #endregion
  
          #region DB Properties 
          // db sets 
  
          #endregion
  
          #region Methods
  
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
  
  
              // ReSharper disable once AssignNullToNotNullAttribute
              optionsBuilder.UseSqlServer("Some-Connection");
  
              base.OnConfiguring(optionsBuilder);
          }
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
  
              // all configutaion 
  
              base.OnModelCreating(modelBuilder);
          }
          #endregion
  
      }
  }
  </code></pre>
  
  <p>Is there anything wrong with code? I am able  to resolver my other repository &amp; services just facing issue with Generic Repository 
  <strong>builder.RegisterGeneric(typeof(Repository&lt;>)).As(typeof(IRepository&lt;>)).InstancePerLifetimeScope();</strong></p>
  
  <p>At time of using this generic repository I gets following error</p>
  
  <blockquote>
    <p>Autofac.Core.DependencyResolutionException: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = CommonService (ReflectionActivator), Services = [MakeTalk.Service.ICommonService], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = Repository<code>1 (ReflectionActivator), Services = [MakeTalk.Data.Infrastructure.IRepository</code>1[[MakeTalk.Model.ExhibitionRequestStatus, MakeTalk.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> No constructors on type 'MakeTalk.Data.Infrastructure.Repository<code>1[MakeTalk.Model.ExhibitionRequestStatus]' can be found with the constructor finder 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'. (See inner exception for details.) (See inner exception for details.) ---&gt; Autofac.Core.DependencyResolutionException: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = Repository</code>1 (ReflectionActivator), Services = [MakeTalk.Data.Infrastructure.IRepository<code>1[[MakeTalk.Model.ExhibitionRequestStatus, MakeTalk.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---&gt; No constructors on type 'MakeTalk.Data.Infrastructure.Repository</code>1[MakeTalk.Model.ExhibitionRequestStatus]' can be found with the constructor finder 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'. (See inner exception for details.) ---> Autofac.Core.DependencyResolutionException: No constructors on type 'MakeTalk.Data.Infrastructure.Repository<code>1[MakeTalk.Model.ExhibitionRequestStatus]' can be found with the constructor finder 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'.
       at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable</code>1 parameters)
       at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable<code>1 parameters)
       --- End of inner exception stack trace ---
       at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable</code>1 parameters)
       at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func<code>1 creator)
       at Autofac.Core.Resolving.InstanceLookup.Execute()
       at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable</code>1 parameters)
       at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
       at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable<code>1 parameters)
       at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable</code>1 parameters)
       --- End of inner exception stack trace ---
       at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable<code>1 parameters)
       at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func</code>1 creator)
       at Autofac.Core.Resolving.InstanceLookup.Execute()
       at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable<code>1 parameters)
       at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable</code>1 parameters)
       at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable<code>1 parameters, Object&amp; instance)
       at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable</code>1 parameters)
       at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
       at lambda_method(Closure , IServiceProvider , Object[] )
       at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.&lt;>c__DisplayClass4_0.b__0(ControllerContext controllerContext)
       at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.&lt;>c__DisplayClass5_0.g__CreateController|0(ControllerContext controllerContext)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
       at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
       at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
       at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)</p>
  </blockquote>