2014-02-08 1 views
0

У меня есть этот класс для инстанцирования моих контроллеров MVC (я использую MVC 5, но не с помощью WebAPI)Ошибки активации HomeController

общественного класс 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. 

Что я делаю не так?

ответ

2

Вы, похоже, довольно быстро избавляетесь от ядра (как только вы его создали). Избавиться от пункта using в вашем RegisterInjections метода:

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

При выбрасывании ядра, вы в основном убиваете все, что вы могли бы прописали в нем, и к тому времени выполнение должно создать экземпляр HomeController и спрашивает ядро для экземпляра IAlbumSevrice ядро ​​уже мертво и не может найти такой экземпляр.

Смежные вопросы