title: Registering Generic Interfaces and classes with Autofac
body: <p>My current Autofac config is working to resolve my ApiControllers in a WebApi.</p>
  
  <p>Where I'm struggling is, I'm attempting to create a 'BaseApiController' with generic constructor parameters, but getting exception: </p>
  
  <blockquote>
    <p>No constructors on type 'Service`1[WorldRegion]' can be found with the constructor finder 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'.</p>
  </blockquote>
  
  <p>Here is the code structure:</p>
  
  <pre><code>public interface IEntityKey { }
  public class WorldRegion : IEntityKey { }
  public interface IRepository&lt;T&gt; where T : IEntityKey { }
  public interface IService&lt;T&gt; where T : IEntityKey { }
  public abstract class Service&lt;T&gt; : IService&lt;T&gt; where T : IEntityKey { }
  public interface IWorldRegionService : IService&lt;WorldRegion&gt; { }
  public class WorldRegionService : Service&lt;WorldRegion&gt;, IWorldRegionService
  {
      private readonly IRepository&lt;WorldRegion&gt; _repository;
  }
  </code></pre>
  
  <p>WORKING API Controller:</p>
  
  <pre><code>public class WorldRegionsController : BaseApiController
  {
      private readonly IWorldRegionService _worldRegionService;
      private readonly ICultureService _cultureService;
  
      public WorldRegionsController(IWorldRegionService worldRegionService, ICultureService cultureService)
      {
          _worldRegionService = worldRegionService;
          _cultureService = cultureService;
      }
  }
  </code></pre>
  
  <p>WORKING Autofac config:</p>
  
  <pre><code>public static void Register(HttpConfiguration config, IAppBuilder app)
  {
      var builder = new ContainerBuilder();
      builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
  
      RegisterTypes(builder);
  
      var container = builder.Build();
      config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
  
      app.UseAutofacMiddleware(container);
      app.UseAutofacWebApi(config);
  }
  
  public static void RegisterTypes(ContainerBuilder builder)
  {
      // Context
      builder.RegisterType&lt;DataContext&gt;().As&lt;IDataContext&gt;().InstancePerRequest();
      // UOW
      builder.RegisterType&lt;UnitOfWork&gt;().As&lt;IUnitOfWork&gt;().InstancePerRequest();
      // Repositories
      builder.RegisterGeneric(typeof(Repository&lt;&gt;)).As(typeof(IRepository&lt;&gt;)).InstancePerRequest();
      // Services
      builder.RegisterType&lt;CultureService&gt;().As&lt;ICultureService&gt;().InstancePerRequest();
      builder.RegisterType&lt;WorldRegionService&gt;().As&lt;IWorldRegionService&gt;().InstancePerRequest();
  }
  </code></pre>
  
  <p>Here is the generic ATTEMPT:</p>
  
  <pre><code>// BaseApiController
  public abstract class BaseApiController&lt;T&gt; : ApiController where T : IEntityKey
  
  {
      protected readonly IService&lt;T&gt; _service;
      protected readonly ICultureService _cultureService;
      public BaseApiController(IService&lt;T&gt; service, ICultureService cultureService)
      {
          _service = service;
          _cultureService = cultureService;
      }
  }
  
  // ApiController
  public class WorldRegionsController : BaseApiController&lt;WorldRegion&gt;
  {
      public WorldRegionsController(
          IService&lt;WorldRegion&gt; service, ICultureService cultureService)
              : base(service, cultureService) {}
  }
  
  // Added to Autofac config
  builder.RegisterGeneric(typeof(Service&lt;&gt;)).As(typeof(IService&lt;&gt;)).InstancePerRequest();
  // Removed
  builder.RegisterType&lt;WorldRegionService&gt;().As&lt;IWorldRegionService&gt;().InstancePerRequest();
  </code></pre>
  
  <p>With this change, I'm getting the exception message noted above (in yellow).  I think I'm missing something in the Autofac config, just not sure what. Maybe include/register/add 'WorldRegion' somehow.</p>
  
  <p>How should I register my types ?</p>