.net - Ninject won't resolve constructor dependencies - Web API 2 -


i've removed unity , attempting use ninject in place. part works, cannot play nice web api 2.

i have ninjectwebcommon.cs file (which have not changed aside adding registrations work fine if resolve explicitly), can see firing start event. when attempt access web api controller moans there being no public parameterless constructor.

an error has occurred.","exceptionmessage":"an error occurred when trying create controller of type 'placeordercontroller'. make sure controller has parameterless public constructor. 

i have these nuget packages installed (fresh yesterday, should latest versions):

ninject

ninject.extensions.conventions

ninject.web.common

ninject.web.common.webhost

ninject.web.webapi

ninject.web.webapi.webhost

i'm not doing clever @ stage, web api controller constructor takes dependency implementing interface. unity had working in minutes, ninject seems configuration nightmare!

what additional steps have missed?

ninjectwebcommons.cs

public static class ninjectwebcommon  {     static ninjectwebcommon()     {         kernel = new interceptallmodule();     }      private static readonly bootstrapper bootstrapper = new bootstrapper();      public static void start()     {         dynamicmoduleutility.registermodule(typeof(oneperrequesthttpmodule));         dynamicmoduleutility.registermodule(typeof(ninjecthttpmodule));         bootstrapper.initialize(() => createkernel(kernel));     }      public static void stop()     {         bootstrapper.shutdown();     }      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;         }     }      private static void registerservices(ikernel kernel)     {         kernel.bind<myinterface>().to<myclass>().insingletonscope();     } } 

controller:

public mycontroller : apicontroller {     private readonly myinterface _myinterface;      public mycontroller(myinterface myinterface)     {         _myinterface = myinterface;     }      . . . } 

you've not setup dependencyresolver correctly. tried couple of ninject packages not work because of incompatible versions of ninject , webapi (and mvc). post: http://www.codeproject.com/articles/412383/dependency-injection-in-asp-net-mvc-and-webapi-us describes in detail how setup webapi use ninject dependency resolver.


Comments