【问题标题】:Error activating HomeController激活 HomeController 时出错
【发布时间】:2014-03-06 08:05:27
【问题描述】:

我有这个类用于实例化我的 MVC 控制器(我使用的是 MVC 5 但不使用 WebApi)

公共类 NinjectControllerFactory : DefaultControllerFactory { 私有只读IKernel _ninjectKernel;

public NinjectControllerFactory(IKernel kernel)
{
    _ninjectKernel = kernel;
}

protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
    return (controllerType == null) ? null : (IController)_ninjectKernel.Get(controllerType);
} }

在 App_Start 我有这个类

public static class NinjectConfig
    {
        public static void RegisterInjections()
        {
            using (IKernel kernel = new StandardKernel())
            {
                ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(kernel));
                kernel.Bind<IAlbumService>().To<AlbumService>();
            }
        }
    }

在 Global.asax 我有

   NinjectConfig.RegisterInjections();

当我运行应用程序时出现此错误

Error activating HomeController
No matching bindings are available, and the type is not self-bindable.
Activation path:
 1) Request for HomeController

Suggestions:
 1) Ensure that you have defined a binding for HomeController.
 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
 3) Ensure you have not accidentally created more than one kernel.
 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
 5) If you are using automatic module loading, ensure the search path and filters are correct.

我做错了什么?

【问题讨论】:

    标签: ninject asp.net-mvc-5


    【解决方案1】:

    您似乎很快就处理掉了内核(一旦您创建了它)。去掉 RegisterInjections 方法中的 using 子句:

    public static void RegisterInjections()
    {
        IKernel kernel = new StandardKernel();
        ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(kernel));
        kernel.Bind<IAlbumService>().To<AlbumService>();
    }
    

    当您处理内核时,您基本上是在杀死您可能在其中注册的所有内容,并且当运行时需要实例化您的 HomeController 并向内核请求IAlbumSevrice 的实例时,内核已经死了并且不能找到这样的实例。

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 2019-12-01
      相关资源
      最近更新 更多