Autofac with MVC4: controller does not have a default constructor

Vocte picture Vocte · Nov 16, 2012 · Viewed 12.2k times · Source

I've been working with Autofac in MVC3 and love it. Now I am trying to implement it with MVC4.

I installed the pre-release versions of Autofac MVC4 and Autofac WebApi through the Package Manager Console (Install-Package Autofac.Mvc4 -Pre and Install-Package Autofac.WebApi -Pre)

I adjusted my IoC container as following:

    private static void SetAutofacContainer()
    {
        var builder = new ContainerBuilder();

        builder.RegisterControllers(Assembly.GetExecutingAssembly());
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest().InstancePerApiRequest();
        builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest().InstancePerApiRequest();
        builder.RegisterType<RepositoryWrapper>().As<RepositoryWrapper>().InstancePerHttpRequest().InstancePerApiRequest();
        builder.RegisterType<ServiceWrapper>().As<ServiceWrapper>().InstancePerHttpRequest().InstancePerApiRequest();

        // Repositories
        builder.RegisterAssemblyTypes(typeof(UserRepository).Assembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces().InstancePerHttpRequest().InstancePerApiRequest();

        // Services
        builder.RegisterAssemblyTypes(typeof(UserService).Assembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerHttpRequest().InstancePerApiRequest();

        IContainer container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

    }

When I run the application (by accessing the API controller) I get the exception: "Controllers.UserController' does not have a default constructor"

The controller looks like this:

namespace Controllers
{
[Authorize]
public class UserController : ApiController
{
    private ServiceWrapper _services;

    public UserController(ServiceWrapper services)
    {
        _services = services;
    }

    // GET api/user/{userrequest}
    public IQueryable<User> Get(UserRequest request)
    {
        if (ModelState.IsValid)
        {
          '...
        }  
    } 
}

Am I missing something? Did I not set it up right? Any help would be greatly appreciated!

Update

My API controller are within a separate project in the same solution. If I place the API controller in my main MVC project, it works. Could someone please enlighten me on how to get Autofac to register the API controllers in my API project?

Answer

nemesv picture nemesv · Nov 17, 2012

With the RegisterApiControllers method you tell Autofac where (in which assembly) it should look for your ApiControllers

So the following call:

builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

Registers the ApiControllers from the current assembly (project).

If you have ApiControllers also in a different project you need to use it like this:

builder.RegisterApiControllers(typeof(UserController).Assembly);

Which means: register all the ApiController form the assembly (project) where the UserController lives. So you only need one RegisterApiControllers per assembly even if you have multiple ApiController in an assembly (project).