Ninject for Web Api 2 is not working in ASP.NET MVC -
i developing asp.net mvc application. in application, need provide rest api. added web api 2 existing mvc application. before added web api 2, using ninject dependency injection. installed via nuget package. whole website developed , working. problem started when added web api 2 project. ninject mvc cannot used web api. installed ninject web api 2. ninjectwebcommon class has been changed after installed it.
this ninjectwebcommon file in app_start folder
[assembly: webactivatorex.preapplicationstartmethod(typeof(ayardirectory.web.app_start.ninjectwebcommon), "start")] [assembly: webactivatorex.applicationshutdownmethodattribute(typeof(ayardirectory.web.app_start.ninjectwebcommon), "stop")] namespace ayardirectory.web.app_start { using system; using system.web; using microsoft.web.infrastructure.dynamicmodulehelper; using ninject; using ninject.web.common; public static class ninjectwebcommon { private static readonly bootstrapper bootstrapper = new bootstrapper(); /// <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(); try { kernel.bind<func<ikernel>>().tomethod(ctx => () => new bootstrapper().kernel); kernel.bind<ihttpmodule>().to<httpapplicationinitializationhttpmodule>(); registerservices(kernel); return kernel; } catch { kernel.dispose(); throw; } } /// <summary> /// load modules or register services here! /// </summary> /// <param name="kernel">the kernel.</param> private static void registerservices(ikernel kernel) { system.web.mvc.dependencyresolver.setresolver(new ayardirectory.web.infrastructure.ninjectdependencyresolver(kernel)); } } }
i changed place after installed ninject web api 2.then run application. website working fine. website still working. web api not working.
this resolver class
namespace ayardirectory.web.infrastructure { public class ninjectdependencyresolver : idependencyresolver { private ikernel kernel; public ninjectdependencyresolver(ikernel kernelparam) { kernel = kernelparam; addbindings(); } public object getservice(type servicetype) { return kernel.tryget(servicetype); } public ienumerable<object> getservices(type servicetype) { return kernel.getall(servicetype); } private void addbindings() { kernel.bind<icategoryrepo>().to<categoryrepo>(); } } }
i dependency injection in api controller this
public class regionscontroller : apicontroller { private iregionrepo regionrepo; private regionscontroller(iregionrepo regionparam) { this.regionrepo = regionparam; } . . . }
when access 1 of action of api controller, giving me following error.
{"message":"an error has occurred.","exceptionmessage":"an error occurred when trying create controller of type 'regionscontroller'. make sure controller has parameterless public constructor.","exceptiontype":"system.invalidoperationexception","stacktrace":" @ system.web.http.dispatcher.defaulthttpcontrolleractivator.create(httprequestmessage request, httpcontrollerdescriptor controllerdescriptor, type controllertype)\r\n @ system.web.http.controllers.httpcontrollerdescriptor.createcontroller(httprequestmessage request)\r\n @ system.web.http.dispatcher.httpcontrollerdispatcher.sendasynccore(httprequestmessage request, cancellationtoken cancellationtoken)\r\n @ system.web.http.dispatcher.httpcontrollerdispatcher.<sendasync>d__0.movenext()","innerexception":{"message":"an error has occurred.","exceptionmessage":"type 'ayardirectory.web.controllers.api.regionscontroller' not have default constructor","exceptiontype":"system.argumentexception","stacktrace":" @ system.linq.expressions.expression.new(type type)\r\n @ system.web.http.internal.typeactivator.create[tbase](type instancetype)\r\n @ system.web.http.dispatcher.defaulthttpcontrolleractivator.getinstanceoractivator(httprequestmessage request, type controllertype, func`1& activator)\r\n @ system.web.http.dispatcher.defaulthttpcontrolleractivator.create(httprequestmessage request, httpcontrollerdescriptor controllerdescriptor, type controllertype)"}}
so please, why not working? missing or wrong code? please how can fix it? how can use ninject web api 2 please? using mvc 5.
Comments
Post a Comment