与MVC4 Autofac:控制器没有默认构造函数 [英] Autofac with MVC4: controller does not have a default constructor
问题描述
我一直使用Autofac在MVC3和喜欢它。现在我想用MVC4来实现它。
I've been working with Autofac in MVC3 and love it. Now I am trying to implement it with MVC4.
我安装的是pre释放Autofac MVC4和Autofac的WebAPI通过包管理器控制台版本(安装封装Autofac.Mvc4 - pre和安装封装Autofac.WebApi - pre)
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)
我调整我的IoC容器如下:
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);
}
当我运行应用程序(通过访问API控制器)我得到异常:
Controllers.UserController'没有默认的构造函数
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)
{
'...
}
}
}
我缺少的东西吗?我不是设置它吧?任何帮助将大大AP preciated!
Am I missing something? Did I not set it up right? Any help would be greatly appreciated!
更新
我的API控制器都在同一个解决方案,一个单独的项目中。如果我把我的主要MVC项目的API控制器,它的工作原理。可能有人请赐教如何让Autofac在我的API项目注册API控制器?
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?
推荐答案
通过你告诉Autofac的 RegisterApiControllers
方法,其中(其中组装)看起来应该为你的 ApiControllers
With the RegisterApiControllers
method you tell Autofac where (in which assembly) it should look for your ApiControllers
所以下面的电话:
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
在注册 ApiControllers
从目前的组件(项目)。
Registers the ApiControllers
from the current assembly (project).
如果您有 ApiController
S还你需要使用它这样一个不同的项目:
If you have ApiController
s also in a different project you need to use it like this:
builder.RegisterApiControllers(typeof(UserController).Assembly);
这意味着:注册全部的 ApiController
形成组件(项目),其中 UserController的
生活。所以,你只需要一个 RegisterApiControllers
每即使你有多个 ApiController
在装配(项目)组装。
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).
这篇关于与MVC4 Autofac:控制器没有默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!