【发布时间】:2015-01-20 10:32:56
【问题描述】:
我得到'..确保控制器有一个无参数的公共构造函数 autofac..'
我的控制器:
public class AccountController : ApiController
{
private IAccountService _accountService;
public AccountController(IAccountService accountService)
{
_accountService = accountService;
}
...
}
我的 autofac 配置:
protected void Application_Start()
{
var builder = new ContainerBuilder();
var config = GlobalConfiguration.Configuration;
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<AccountService>().As<IAccountService>().InstancePerRequest();
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
我的代码有什么问题?
完整的错误信息:
Exception=System.InvalidOperationException: An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor. ---> Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'MyProj.Api.Controllers.AccountController' can be invoked with the available services and parameters:
Cannot resolve parameter 'MyProj.Engine.Services.Interfaces.IAccountService accountService' of constructor 'Void .ctor(MyProj.Engine.Services.Interfaces.IAccountService)'.
【问题讨论】:
-
您是否安装了 Web API 集成 'Autofac.WebApi' nuget 包?
-
@MilenPavlov 是的,当然grab.by/DWrG
-
@MilenPavlov 我添加了内部异常消息,可能会有所帮助:)
-
你能在这里显示 AccountService 的构造函数吗?
-
你可以试试这一行来注册你的控制器 builder.RegisterApiControllers(typeof(AccountController).Assembly); ?
标签: asp.net-mvc autofac asp.net-web-api2 azure-mobile-services