title: Autofac and ASP .Net MVC 4 Web API
body: <p>I am using <strong>Autofac</strong> for IoC in my ASP .Net MVC 4 project. Autofac is having some trouble initializing the repository and passing it to the <strong>API Controller</strong>.</p>
  
  <p>I am sure I am missing something in my configuration.</p>
  
  <p>Here is the error I get when I navigate to: <code>https://localhost:44305/api/integration</code></p>
  
  <pre><code>&lt;Error&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 'EL.Web.Controllers.API.IntegrationController' can be invoked with 
          the available services and parameters: Cannot resolve parameter 
          'EL.Web.Infrastructure.IRepository`1[EL.Web.Models.Integration] repository' of 
          constructor 'Void .ctor(EL.Web.Infrastructure.IRepository`1[EL.Web.Models.Integration])'.
      &lt;/ExceptionMessage&gt;
      &lt;ExceptionType&gt;Autofac.Core.DependencyResolutionException&lt;/ExceptionType&gt;
      &lt;StackTrace&gt;
          at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) 
          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.ResolveComponent(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, 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;/Error&gt;
  </code></pre>
  
  <p>Here are some relevant bits of code:</p>
  
  <p><strong>IoC Bootstrapper:</strong></p>
  
  <pre><code>public static class Bootstrapper
  {
      public static void Initialize()
      {
          var builder = new ContainerBuilder();
  
          builder.RegisterControllers(Assembly.GetExecutingAssembly());
          builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
  
          builder.Register(x =&gt; new SharePointContext(HttpContext.Current.Request)).As&lt;ISharePointContext&gt;().SingleInstance();
          builder.RegisterType&lt;SharePointRepository&lt;IEntity&gt;&gt;().As&lt;IRepository&lt;IEntity&gt;&gt;();
          builder.RegisterType&lt;SharePointContextFilter&gt;().SingleInstance();
  
          builder.RegisterFilterProvider();
  
          IContainer container = builder.Build();
          DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  
          var resolver = new AutofacWebApiDependencyResolver(container);
          GlobalConfiguration.Configuration.DependencyResolver = resolver;
      }
  }
  </code></pre>
  
  <p><strong>IRepository:</strong></p>
  
  <pre><code>public interface IRepository&lt;T&gt;
  {
      void Add(T entity);
  
      void Delete(int id);
  
      IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; filter = null);
  
      void Update(int id, T entity);
  }
  </code></pre>
  
  <p><strong>SharePointRepository:</strong></p>
  
  <pre><code>internal class SharePointRepository&lt;T&gt; : IRepository&lt;T&gt; where T : IEntity
  {
      private readonly ISharePointContext _context;
      private readonly string _listName;
  
      internal SharePointRepository(ISharePointContext context)
      {
          _context = context;
  
          object[] attributes = typeof (T).GetCustomAttributes(typeof (SharePointListAttribute), false);
  
          if (!attributes.Any())
          {
              throw new Exception("No associated SharePoint list defined for " + typeof (T));
          }
  
          _listName = ((SharePointListAttribute) attributes[0]).ListName;
      }
  
      public void Add(T entity)
      {
          throw new NotImplementedException();
      }
  
      public void Delete(int id)
      {
          throw new NotImplementedException();
      }
  
      public IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; filter)
      {
          throw new NotImplementedException();
      }
  
      public void Update(int id, T entity)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p><strong>IntegrationController:</strong></p>
  
  <pre><code>public class IntegrationController : ApiController
  {
      private readonly IRepository&lt;Integration&gt; _repository;
  
      public IntegrationController(IRepository&lt;Integration&gt; repository)
      {
          _repository = repository;
      }
  
      public void Delete(Guid integrationId)
      {
          _repository.Delete(Get(integrationId).Id);
      }
  
      public IEnumerable&lt;Integration&gt; Get()
      {
          return _repository.Find();
      }
  
      public Integration Get(Guid integrationId)
      {
          return _repository.Find(i =&gt; i.IntegrationId == integrationId).FirstOrDefault();
      }
  
      public void Post([FromBody] Integration integration)
      {
          _repository.Add(integration);
      }
  
      public void Put(Guid integrationId, [FromBody] Integration integration)
      {
          _repository.Update(Get(integrationId).Id, integration);
      }
  }
  </code></pre>
  
  <p><strong>IEntity:</strong></p>
  
  <pre><code>internal interface IEntity
  {
      int Id { get; }
  }
  </code></pre>
  
  <p><strong>Entity:</strong></p>
  
  <pre><code>public abstract class Entity : IEntity
  {
      protected Entity(int id)
      {
          Id = id;
      }
  
      public int Id { get; private set; }
  }
  </code></pre>
  
  <p><strong>Integration:</strong></p>
  
  <pre><code>[SharePointList("Integrations")]
  public class Integration : Entity
  {
      public Integration(int id) : base(id)
      {
      }
  
      public string ApiUrl { get; set; }
  
      public bool DeletionAllowed { get; set; }
  
      public Guid IntegrationId { get; set; }
  
      public string Key { get; set; }
  
      public string List { get; set; }
  
      public bool OutgoingAllowed { get; set; }
  
      public string RemoteWeb { get; set; }
  
      public string Web { get; set; }
  }
  </code></pre>