20

In MVC I simply make the class NinjectControllerFactory that implements DefaultControllerFactory interface then do some bindings in it. at last in Global I run it:

ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());

But what about using Ninject in ASP.NET Web API? there are some information on the web but are out dated and for pre-released versions.

Is there a straightforward way for this problem?

Blazi
  • 991
  • 3
  • 9
  • 19

4 Answers4

19

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:

  1. http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/
  2. http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/
Mark Jones
  • 12,156
  • 2
  • 50
  • 62
  • 1
    It looks like there's an update on two different Nuget packages: [Ninject.Web.WebApi](http://nuget.org/packages/Ninject.Web.WebApi/3.0.0.2) and [Ninject.Web.WebApi.Updated](http://nuget.org/packages/Ninject.Web.WebApi.Updated/). It seems that the first one is still in a pre-release version. – Christopher Stevenson Apr 25 '13 at 14:16
  • Solution from peterprovost article seems to break InSingletonScope bindings, essentially making everything per-http-request. 'Official' webapi support in ninject seem to be nowhere near production, so will be switching to Auotfac that seem to support WebApi (and more) out of the box: https://code.google.com/p/autofac/wiki/WebApiIntegration – Dmitry Jun 28 '13 at 23:16
6

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.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • 3
    You will also need to follow instructions on http://stackoverflow.com/questions/22768721/parameterless-constructor-error-with-ninject-bindings-in-net-web-api-2-1 – P6345uk Mar 31 '15 at 17:06
6

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.

Bryan
  • 5,065
  • 10
  • 51
  • 68
  • This seems to be outdated, because you now get an error that `NinjectDependencyResolver` cannot be found. – Florian Winter Nov 22 '22 at 09:09
  • EDIT: It does work after adding `using Ninject.Web.WebApi;`. Strangely, I have another ASP.NET WebApi2 application which works without adding this line. – Florian Winter Nov 22 '22 at 09:25
1

The following steps work like a sharm to get Ninject working on an WebAPI project:

  1. Install the Ninject.Web.WebApi NuGet package.
  2. Install the Ninject.Web.WebApi.WebHost NuGet package.
  3. Register your dependencies in the method "RegisterServices" in the file NinjectWebCommon added to the App_Start folder. Like this:

Private static void RegisterServices(IKernel kernel)
{

  kernel.Bind <IFoo>().To <Foo>();

} 
D.B
  • 4,009
  • 14
  • 46
  • 83