15

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?

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
Vocte
  • 317
  • 1
  • 5
  • 11

1 Answers1

25

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).

nemesv
  • 138,284
  • 16
  • 416
  • 359
  • You mean `RegisterControllers`? There doesn't seem to be `RegisterApiControllers` method. – Andriy Drozdyuk Jul 18 '13 at 12:48
  • 3
    @drozzy there is two methods `RegisterControllers` for the MVC controllers and `RegisterApiController` for the web.api controllers. The `RegisterApiController` only available if you use the Autofac.WebAPi extension. – nemesv Jul 18 '13 at 12:59
  • @nemesv D'oh, thanks! Even though I did have to call both `RegisterControllers` and `RegisterApiControllers`. Weird. – Andriy Drozdyuk Jul 18 '13 at 13:01
  • @nemesv do you know if there is any issue doing: builder.RegisterControllers(System.Reflection.Assembly.GetExecutingAssembly()); builder.RegisterApiControllers(System.Reflection.Assembly.GetExecutingAssembly()); At the same time? – Campinho Apr 15 '19 at 04:32