【问题标题】:Autofac Azure Mobile Service integration issueAutofac Azure 移动服务集成问题
【发布时间】: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


【解决方案1】:

修复了我自己。实际上问题在于该项目是 Azure 移动服务。 这是我第一次使用它,我已经像以前为 WebApi 所做的那样对其进行了配置。但正如我在上次基于 OWIN 谷歌搜索此项目类型后所了解的那样。 这是一篇关于:http://blogs.msdn.com/b/azuremobile/archive/2014/04/19/autofac-and-azure-mobile-services-net-backend.aspx

的好文章

所以应该是这样的:

 ConfigOptions options = new ConfigOptions();

 ConfigBuilder builder = new ConfigBuilder(options, (httpConfig, autofac) =>
 {
         autofac.RegisterInstance(new MyService()).As<IMyService>();
 });

 HttpConfiguration config = ServiceConfig.Initialize(builder);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-28
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多