c# - Bootstrapper does not load my NinjectModules -
i trying use bootstrapper ninject. have installed ninject.mvc3
initialize ninject container nuget , have created test module this:
public class testninjectmodule : ninjectmodule { public override void load() { bind<dbentities>().toself().inrequestscope(); bind(typeof(igenericrepository<>)) .to(typeof(genericrepository<>)).inrequestscope(); bind<isystemrepository>().to<systemrepository>().inrequestscope(); bind<iunitofwork>().to<unitofwork>(); } }
i have included start()
call in global.asax.cs
public class mvcapplication : system.web.httpapplication { protected void application_start() { arearegistration.registerallareas(); webapiconfig.register(globalconfiguration.configuration); filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes); bundleconfig.registerbundles(bundletable.bundles); bootstrapper.with.ninject().start(); bootstrapper.with.automapper().start(); } }
however still activationexception
ninject when controller dependency called, signalling testninjectmodule.load()
method not called. how can solve that?
exception message:
error activating iunitofwork no matching bindings available, , type not self-bindable. activation path: 2) injection of dependency iunitofwork parameter unitofwork of constructor of type > treasurysystemcontroller 1) request systemcontroller
suggestions: 1) ensure have defined binding iunitofwork. 2) if binding defined in module, ensure module has been loaded > kernel. 3) ensure have not accidentally created more 1 kernel. 4) if using constructor arguments, ensure parameter name matches >constructors parameter name. 5) if using automatic module loading, ensure search path , filters >correct.
stack trace:
at ninject.kernelbase.resolve(irequest request) in c:\projects\ninject\ninject\src\ninject\kernelbase.cs:line 359 @ ninject.planning.targets.target
1.getvalue(type service, icontext parent) in c:\projects\ninject\ninject\src\ninject\planning\targets\target.cs:line 197 @ ninject.planning.targets.target
1.resolvewithin(icontext parent) in c:\projects\ninject\ninject\src\ninject\planning\targets\target.cs:line 165 @ ninject.activation.providers.standardprovider.getvalue(icontext context, itarget target) in c:\projects\ninject\ninject\src\ninject\activation\providers\standardprovider.cs:line 114 @ ninject.activation.providers.standardprovider.<>c_displayclass4.b_2(itarget target) in c:\projects\ninject\ninject\src\ninject\activation\providers\standardprovider.cs:line 96 @ system.linq.enumerable.whereselectarrayiterator2.movenext() @ system.linq.buffer
1..ctor(ienumerable1 source) @ system.linq.enumerable.toarray[tsource](ienumerable
1 source) @ ninject.activation.providers.standardprovider.create(icontext context) in c:\projects\ninject\ninject\src\ninject\activation\providers\standardprovider.cs:line 96 @ ninject.activation.context.resolve() in c:\projects\ninject\ninject\src\ninject\activation\context.cs:line 157 @ ninject.kernelbase.<>c_displayclass10.b_c(ibinding binding) in c:\projects\ninject\ninject\src\ninject\kernelbase.cs:line 386 @ system.linq.enumerable.whereselectenumerableiterator2.movenext() @ system.linq.enumerable.singleordefault[tsource](ienumerable
1 source) @ ninject.web.mvc.ninjectdependencyresolver.getservice(type servicetype) in c:\projects\ninject\ninject.web.mvc\mvc3\src\ninject.web.mvc\ninjectdependencyresolver.cs:line 56 @ system.web.mvc.defaultcontrollerfactory.defaultcontrolleractivator.create(requestcontext requestcontext, type controllertype)
i have found solution. changing implementation of ninjectwebcommon
to:
public static class ninjectwebcommon { private static readonly bootstrapper bootstrapper = new bootstrapper(); public static ikernel kernel { get; private set; } // expose kernel through property /// <summary> /// starts application /// </summary> public static void start() { dynamicmoduleutility.registermodule(typeof(oneperrequesthttpmodule)); dynamicmoduleutility.registermodule(typeof(ninjecthttpmodule)); bootstrapper.initialize(createkernel); } /// <summary> /// stops application. /// </summary> public static void stop() { bootstrapper.shutdown(); } /// <summary> /// creates kernel manage application. /// </summary> /// <returns>the created kernel.</returns> private static ikernel createkernel() { var kernel = new standardkernel(); kernel.bind<func<ikernel>>().tomethod(ctx => () => new bootstrapper().kernel); kernel.bind<ihttpmodule>().to<httpapplicationinitializationhttpmodule>(); return kernel = kernel; } }
then in global.asax.cs
:
public class mvcapplication : system.web.httpapplication { protected void application_start() { arearegistration.registerallareas(); webapiconfig.register(globalconfiguration.configuration); filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes); bundleconfig.registerbundles(bundletable.bundles); bootstrapper.with.automapper().start(); bootstrapper.with.ninject() .withcontainer(ninjectwebcommon.kernel) // use kernel inside ninjectwebcommon instead of creating new 1 .start(); } }
then classes deriving ninjectmodule
have load()
methods called.
Comments
Post a Comment