title: Autofac with web api not resolving dependencies
body: <p>I'm having a hard time trying to understand why I can't seem to get autofac 3.5.2 (also tried with version 4.4) to work with Web Api 2.2 + Owin.</p>
  
  <p><strong>I have a controller that requires a dependency of <code>IClass1</code>. It appears to be registered, but for some reason it fails to resolve at runtime. Any idea what the problem might be?</strong></p>
  
  <p>The registration in question appears to be there (see line 4 - <code>Class1</code> registered as <code>IClass1</code>):</p>
  
  <p><a href="https://i.stack.imgur.com/wPxMl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wPxMl.png" alt="Registrations"></a></p>
  
  <p>And the lifetime scope is right, it is set to: AutofacWebRequest</p>
  
  <p><a href="https://i.stack.imgur.com/9srGS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9srGS.png" alt="enter image description here"></a></p>
  
  <h1>Code</h1>
  
  <h2>Registrations</h2>
  
  <pre><code>public void Configuration(IAppBuilder app)
  {
      var suffix = typeof(DefaultHttpControllerSelector).GetField("ControllerSuffix", BindingFlags.Static | BindingFlags.Public);
  
      if (suffix != null) suffix.SetValue(null, string.Empty);
  
      var container = InitializeDiContainer(app);
      var config = new HttpConfiguration
      {
          DependencyResolver = new AutofacWebApiDependencyResolver(container)
      };
  
      config.MapHttpAttributeRoutes();
  
      app.UseAutofacMiddleware(container);
      app.UseAutofacWebApi(config);
      app.UseWebApi(config);
  
      Mapper.Initialize(cfg =&gt;
      {
          foreach (var profile in container.Resolve&lt;IEnumerable&lt;Profile&gt;&gt;())
          {
              cfg.AddProfile(profile);
          }
      });
  
      OnEndpointConfigurationCompleted(app, container);
  }
  
  private IContainer InitializeDiContainer(IAppBuilder app)
  {
      var builder = new ContainerBuilder();
      var allAssemblies = AssemblyUtils.GetAllAssemblies();
  
      builder.RegisterApiControllers("Endpoint", AssemblyUtils.GetEndpointAssemblies());
  
      builder.RegisterAssemblyTypes(AssemblyUtils.GetAllAssemblies())
          .Where(t =&gt; typeof(IReadModelService).IsAssignableFrom(t) ||
              typeof(IRepository).IsAssignableFrom(t))
          .AsImplementedInterfaces()
          .InstancePerRequest()
          .PropertiesAutowired();
  
      builder.RegisterAssemblyTypes(AssemblyUtils.GetAllAssemblies())
          .Where(t =&gt; typeof(IProvider).IsAssignableFrom(t))
          .AsImplementedInterfaces()
          .InstancePerRequest()
          .PropertiesAutowired();
  
      var configFilePath = Path.Combine(HttpRuntime.BinDirectory, "endpointConfig.json");
  
      builder.Register(ctx =&gt; JsonConvert.DeserializeObject&lt;Class1Setting&gt;(File.ReadAllText(configFilePath)))
          .InstancePerRequest();
  
      return builder.Build();
  }
  </code></pre>
  
  <h2>Dependencies</h2>
  
  <pre><code>public class Class1 : IClass1
  { }
  
  public interface IClass1 : IReadModelService
  { }
  
  public class Class1Setting
  {
      public string DataSource { get; set; }
  }
  </code></pre>
  
  <h2>Controller</h2>
  
  <p>The controller fails to resolve the <code>IClass1</code> instance.</p>
  
  <pre><code>public class DeviceQueriesEndpoint : ApiController, IDeviceQueriesEndpoint
  {
      private IClass1 _class1;
  
  
      public DeviceQueriesEndpoint(IClass1 class1)
      {
          _class1 = class1;
      }
  }
  </code></pre>
  
  <h1>Exception</h1>
  
  <p>This is the exception I get at runtime:</p>
  
  <p><em>An error occurred when trying to create a controller of type 'DeviceQueriesEndpoint'. Make sure that the controller has a parameterless public constructor.</em></p>
  
  <pre><code>Error&gt;
  &lt;Message&gt;An error has occurred.&lt;/Message&gt;
  &lt;ExceptionMessage&gt;
  An error occurred when trying to create a controller of type 'DeviceQueriesEndpoint'. Make sure that the controller has a parameterless public constructor.
  &lt;/ExceptionMessage&gt;
  &lt;ExceptionType&gt;System.InvalidOperationException&lt;/ExceptionType&gt;
  &lt;StackTrace&gt;
  at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.&lt;SendAsync&gt;d__1.MoveNext()
  &lt;/StackTrace&gt;
  &lt;InnerException&gt;
  &lt;Message&gt;An error has occurred.&lt;/Message&gt;
  &lt;ExceptionMessage&gt;
  An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = DeviceQueriesEndpoint (DelegateActivator), Services = [xpto.Middleware.Api.Device.Endpoint.DeviceQueriesEndpoint], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = ExternallyOwned ---&gt; An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = DeviceQueriesEndpoint (ReflectionActivator), Services = [xpto.Middleware.Api.Device.Endpoint.DeviceQueriesEndpoint], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope ---&gt; None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'xpto.Middleware.Api.Device.Endpoint.DeviceQueriesEndpoint' can be invoked with the available services and parameters: Cannot resolve parameter 'xpto.ClassLibrary1.IClass1 class1' of constructor 'Void .ctor(xpto.ClassLibrary1.IClass1)'. (See inner exception for details.) (See inner exception for details.)
  &lt;/ExceptionMessage&gt;
  &lt;ExceptionType&gt;Autofac.Core.DependencyResolutionException&lt;/ExceptionType&gt;
  &lt;StackTrace&gt;
  at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) at Autofac.Core.Resolving.InstanceLookup.Execute() at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object&amp; instance) at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters) at Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType) at Autofac.Integration.WebApi.AutofacWebApiDependencyScope.GetService(Type serviceType) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1&amp; activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
  &lt;/StackTrace&gt;
  &lt;InnerException&gt;
  &lt;Message&gt;An error has occurred.&lt;/Message&gt;
  &lt;ExceptionMessage&gt;
  An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = DeviceQueriesEndpoint (ReflectionActivator), Services = [xpto.Middleware.Api.Device.Endpoint.DeviceQueriesEndpoint], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope ---&gt; None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'xpto.Middleware.Api.Device.Endpoint.DeviceQueriesEndpoint' can be invoked with the available services and parameters: Cannot resolve parameter 'xpto.ClassLibrary1.IClass1 class1' of constructor 'Void .ctor(xpto.ClassLibrary1.IClass1)'. (See inner exception for details.)
  &lt;/ExceptionMessage&gt;
  &lt;ExceptionType&gt;Autofac.Core.DependencyResolutionException&lt;/ExceptionType&gt;
  &lt;StackTrace&gt;
  at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) at Autofac.Core.Resolving.InstanceLookup.Execute() at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.Core.Registration.ExternalRegistrySource.&lt;&gt;c__DisplayClass2_0.&lt;RegistrationsFor&gt;b__2(IComponentContext c, IEnumerable`1 p) at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
  &lt;/StackTrace&gt;
  &lt;InnerException&gt;
  &lt;Message&gt;An error has occurred.&lt;/Message&gt;
  &lt;ExceptionMessage&gt;
  None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'xpto.Middleware.Api.Device.Endpoint.DeviceQueriesEndpoint' can be invoked with the available services and parameters: Cannot resolve parameter 'xpto.ClassLibrary1.IClass1 class1' of constructor 'Void .ctor(xpto.ClassLibrary1.IClass1)'.
  &lt;/ExceptionMessage&gt;
  &lt;ExceptionType&gt;Autofac.Core.DependencyResolutionException&lt;/ExceptionType&gt;
  &lt;StackTrace&gt;
  at Autofac.Core.Activators.Reflection.ReflectionActivator.GetValidConstructorBindings(IComponentContext context, IEnumerable`1 parameters) at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
  &lt;/StackTrace&gt;
  &lt;/InnerException&gt;
  &lt;/InnerException&gt;
  &lt;/InnerException&gt;
  &lt;/Error&gt;
  </code></pre>
  
  <h1>Things I've Tried</h1>
  
  <p>If I manually register the concrete type it works, which is odd:</p>
  
  <pre><code>builder.RegisterType&lt;Class1&gt;().As&lt;IClass1&gt;();
  </code></pre>
  
  <p>If I remove the constructor and manually try to resolve <code>IClass1</code> inside an action, it will still fail, which makes me believe that there's some kind of bug with the stack combination that I am using.</p>