title: Autofac with Web API 2 - Parameter-less constructor error
body: <p>I am trying to get Autofac setup on my Web API 2 project. I have only used it a couple of times before. I used Nuget an installed both <strong>Autofac</strong> and <strong>Autofac.WebApi2</strong>.
  Then in my <strong>Startup</strong> class I did this:</p>
  
  <pre><code>public partial class Startup
  {
      public void Configuration(IAppBuilder app)
      {
  
          // Get our http configuration
          var config = new HttpConfiguration();
  
          // Register the Autofac middleware FIRST. This also adds
          // Autofac-injected middleware registered with the container.
          var container = ConfigureInversionOfControl(app, config);
  
          // Register all areas
          AreaRegistration.RegisterAllAreas();  
          GlobalConfiguration.Configure(WebApiConfig.Register);
  
          // Use our web api
          app.UseWebApi(config);
      }
  
      /// &lt;summary&gt;
      /// Configures Autofac DI/IoC
      /// &lt;/summary&gt;
      /// &lt;param name="app"&gt;&lt;/param&gt;
      /// &lt;param name="config"&gt;&lt;/param&gt;
      private IContainer ConfigureInversionOfControl(IAppBuilder app, HttpConfiguration config)
      {
  
          // Create our container
          var builder = new ContainerBuilder();
  
          // You can register controllers all at once using assembly scanning...
          builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
  
          // Register our module
          builder.RegisterModule(new AutofacModule());
  
          // Build
          var container = builder.Build();
  
          // Lets Web API know it should locate services using the AutofacWebApiDependencyResolver
          config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
  
          // Return our container
          return container;
      }
  }
  </code></pre>
  
  <p>The module I created looks like this:</p>
  
  <pre><code>public class AutofacModule : Module
  {
      protected override void Load(ContainerBuilder builder)
      {
  
          // Create our Singletons
          builder.RegisterType&lt;DatabaseContext&gt;().As&lt;DbContext&gt;().InstancePerRequest();
  
          // Create our Services
          builder.RegisterType&lt;UnitOfWork&gt;().As&lt;IUnitOfWork&gt;().InstancePerRequest();
          builder.RegisterType&lt;AnswerService&gt;().As&lt;IAnswerService&gt;().InstancePerRequest();
          builder.RegisterType&lt;CategoryService&gt;().As&lt;ICategoryService&gt;().InstancePerRequest();
          builder.RegisterType&lt;FeedService&gt;().As&lt;IFeedService&gt;().InstancePerRequest();
          builder.RegisterType&lt;FilterService&gt;().As&lt;IFilterService&gt;().InstancePerRequest();
          builder.RegisterType&lt;QuestionGroupService&gt;().As&lt;IQuestionGroupService&gt;().InstancePerRequest();
          builder.RegisterType&lt;StateService&gt;().As&lt;IStateService&gt;().InstancePerRequest();
  
          // Create our providers
          //builder.RegisterType&lt;AnswerProvider&gt;().As&lt;IAnswerProvider&gt;().InstancePerLifetimeScope();
          builder.RegisterType&lt;CategoryProvider&gt;().As&lt;ICategoryProvider&gt;().InstancePerRequest();
          builder.RegisterType&lt;FeedProvider&gt;().As&lt;IFeedProvider&gt;().InstancePerRequest();
          builder.RegisterType&lt;FilterProvider&gt;().As&lt;IFilterProvider&gt;().InstancePerRequest();
          builder.RegisterType&lt;QuestionGroupProvider&gt;().As&lt;IQuestionGroupProvider&gt;().InstancePerRequest();
          builder.RegisterType&lt;StateProvider&gt;().As&lt;IStateProvider&gt;().InstancePerRequest();
      }
  }
  </code></pre>
  
  <p>And I added a provider as a parameter for my API constructor like this:</p>
  
  <pre><code>// Readonly properties
  private readonly ICategoryProvider _provider;
  
  /// &lt;summary&gt;
  /// Default constructor
  /// &lt;/summary&gt;
  public CategoriesController(ICategoryProvider provider)
  {
      _provider = provider;
  }
  </code></pre>
  
  <p>But, when I try to call the API (using postman) I get this error:</p>
  
  <pre><code>{
      "message": "An error has occurred.",
      "exceptionMessage": "An error occurred when trying to create a controller of type 'CategoriesController'. Make sure that the controller has a parameterless public constructor.",
      "exceptionType": "System.InvalidOperationException",
      "stackTrace": "   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n   at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.&lt;SendAsync&gt;d__1.MoveNext()",
      "innerException": {
          "message": "An error has occurred.",
          "exceptionMessage": "Type 'Piiick.Api.Controllers.CategoriesController' does not have a default constructor",
          "exceptionType": "System.ArgumentException",
          "stackTrace": "   at System.Linq.Expressions.Expression.New(Type type)\r\n   at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1&amp; activator)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
      }
  }
  </code></pre>
  
  <p>I am now stuck, I can't see where the issue is at all.
  The category provider has a constructor like this:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Default constructor
  /// &lt;/summary&gt;
  public CategoryProvider(IUnitOfWork unitOfWork, ICategoryService service)
  {
  
      // Map our properties
      _unitOfWork = unitOfWork;
      _service = service;
  }
  </code></pre>
  
  <p>and the <strong>CategoryService</strong> has a constructor like this:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// The default constructor
  /// &lt;/summary&gt;
  /// &lt;param name="unitOfWork"&gt;&lt;/param&gt;
  public CategoryService(IUnitOfWork unitOfWork)
      : base(unitOfWork)
  {
  }
  </code></pre>
  
  <p>and finally, the <strong>UnitOfWork</strong> has a constructor like this:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Default constructor
  /// &lt;/summary&gt;
  /// &lt;param name="context"&gt;The database context&lt;/param&gt;
  public UnitOfWork(DbContext context)
  {
      _context = context;
      _repositories = new Dictionary&lt;Type, object&gt;();
  }
  </code></pre>
  
  <p>From what I can tell, everything is set up correctly.
  Can anyone spot anything I have forgotten to do?</p>