In MVC I simply make the class NinjectControllerFactory
that implements DefaultControllerFactory
interface then do some bindings in it. at last in
The reason a lot of the articles are old is because the approach hasn't changed since June 2012 (RC released May 31st). You need to add the Ninject MVC3 Nuget package, then implement 'IDepenencyResolver' and then register your implementation.
The best two walk-thoughs are:
For Web Api 2 you need these nuget packages -
Ninject
Ninject.Web.Common
Ninject.Web.WebApi
Ninject.Web.Common.WebHost
WebActivatorEx
And you need to edit NinjectWebCommon.CreateKernel(..)
to include
RegisterServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
return kernel;
I've written a detailed blog post here - http://NoDogmaBlog.bryanhogan.net/2016/04/web-api-2-and-ninject-how-to-make-them-work-together/ including a full solution to download.
The following steps work like a sharm to get Ninject working on an WebAPI project:
Private static void RegisterServices(IKernel kernel)
{
kernel.Bind <IFoo>().To <Foo>();
}
The current version of Ninject.Web.WebApi, since at least 3.2.1.0, no longer requires anything additional to be added manually. Just add the package and register everything in NinjectWebCommon.cs, as usual.