Autofac with MVC4: controller does not have a default constructor

江枫思渺然 提交于 2019-12-06 17:55:08

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/13423992/autofac-with-mvc4-controller-does-not-have-a-default-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!