title: Autofac 4 with .NetCore API controller not loading
body: <p>I have an instance where my controller is never instantiated when i use <code>Autofac</code>.  I figured I have done something wrong with the configuration, but I cannot figure it out.  I have 2 projects within my solution.  1) API 2) Core   All the models, repository, and services live in the Core.  Only the controllers are in the API.</p>
  
  <p>If I navigate to the default or values controller, they work fine. If I remove the constructor from my <code>MemberController</code>, it will work, but I get <code>NULL</code> references on the service.  If I add the constructor back, the <code>MemberController</code> never loads (break point in constructor and get method) are never hit.</p>
  
  <p>The Service requires a data model on instantiation.  In the instance below, the <code>MemberController</code> takes a <code>MemberService&lt;MemberDM&gt;</code> as an <code>IService&lt;IDataModel&gt;</code>.
  I believe I have registered everything within my <code>AutofacModule</code>, but it doesn't seem to be working, as the constructor is never hit in <code>MemberController</code>.</p>
  
  <p>Any thoughts/help would be much appreciated.</p>
  
  <p>Startup.cs</p>
  
  <pre><code>public class Startup
  {
      public Startup(IHostingEnvironment env)
      {
          var builder = new ConfigurationBuilder()
              .SetBasePath(env.ContentRootPath)
              .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
              .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
              .AddEnvironmentVariables();
          Configuration = builder.Build();
      }
  
      public IContainer ApplicationContainer { get; private set; }
      public IConfigurationRoot Configuration { get; private set; }
  
  
      // This method gets called by the runtime. Use this method to add services to the container.
      public IServiceProvider ConfigureServices(IServiceCollection services)
      {
  
          // Add service and create Policy with options
          services.AddCors(o =&gt; o.AddPolicy("CorsPolicy", p =&gt;
          {
              p.AllowAnyOrigin()
               .AllowAnyMethod()
               .AllowAnyHeader()
               .AllowCredentials();
          }));    
  
          // Add framework services.
          services.AddMvc();
  
          var builder = new ContainerBuilder();
          var connectionString = Configuration.GetValue&lt;string&gt;("DBConnection:ConnectionString");
  
          builder.RegisterModule(new AutofacModule(connectionString));
  
          builder.Populate(services);
          ApplicationContainer = builder.Build();
          return new AutofacServiceProvider(ApplicationContainer);
  
      }
  
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
      {
          loggerFactory.AddConsole(Configuration.GetSection("Logging"));
          loggerFactory.AddDebug();
          app.UseCors("CorsPolicy");
          app.UseMvc();
  
          appLifetime.ApplicationStopped.Register(() =&gt; this.ApplicationContainer.Dispose());
      }
  }
  </code></pre>
  
  <p>AutofacModule</p>
  
  <pre><code>public class AutofacModule :Autofac.Module
  {
      private string _connectionString;
  
      public AutofacModule(string connectionString)
      {
          _connectionString = connectionString;
      }
  
      protected override void Load(ContainerBuilder builder)
      {            
          // Register Connection class and expose IConnection 
          // by passing in the Database connection information
          builder.RegisterType&lt;Connection&gt;() // concrete type
              .As&lt;IConnection&gt;() // abstraction
              .WithParameter("connectionString", _connectionString)
              .InstancePerLifetimeScope();
  
          // Register Repository class and expose IRepository
          builder.RegisterType&lt;Repository&gt;() // concrete type
              .As&lt;IRepository&gt;() // abstraction
              .InstancePerLifetimeScope();
  
          // Register DataModel as IDataModel 
          builder.RegisterAssemblyTypes(typeof(IServiceAssembly).GetTypeInfo().Assembly)
              .Where(t =&gt; t.Name.EndsWith("DM"))
              //.AsImplementedInterfaces();
              .As&lt;IDataModel&gt;();
  
          // Register Service Class as IService
          builder.RegisterAssemblyTypes(typeof(IServiceAssembly).GetTypeInfo().Assembly)
              .Where(t =&gt; t.Name.EndsWith("Service"))
              .Except&lt;IService&lt;IDataModel&gt;&gt;()
              //.AsImplementedInterfaces();
              .As&lt;IService&lt;IDataModel&gt;&gt;();
      }
  }
  </code></pre>
  
  <p>IServiceAssembly</p>
  
  <pre><code> public interface IServiceAssembly
  {
  }
  </code></pre>
  
  <p>MemberController</p>
  
  <pre><code> [Route("api/[controller]")]
  public class MemberController : Controller
  {
  
      private readonly IService&lt;MemberDM&gt; _memberService;
  
      public MemberController(IService&lt;MemberDM&gt; service)
      {
          _memberService = service;
      }
  
      // GET: api/values
      [HttpGet]
      public IEnumerable&lt;string&gt; Get()
      {
          return new string[] { "value1", "value2" };
      }
  
      // GET api/values/5
      [HttpGet("{id}")]
      public async Task&lt;IActionResult&gt; Get(int id)
      {
          var result = await _memberService.Get(id);
          return View(result);
      }}
  </code></pre>