8

I am building an application using Ninject (MVC5 + WEBAPI). And having some issues figuring out which ones of the many answers explain the most up to date solution to integrate Ninject and WebApi. So I have the following packages installed:

  • Ninject
  • Ninject Integration for WebApi2
  • Ninject.MVC5
  • Ninject.Web.Common
  • Ninject.Web.Common.WebHost

Some people suggest that my initial problem (Ninject is not resolving the API controllers instantiation) should be resolved by either:

  • Installing another package (Ninject.Web.WebApi-RC, Ninject.WebAPI, etc.)
  • Creating my own dependency resolver
  • Installing Ninject Owin host
  • Mix any of the above and hope it finally works.

So it comes down to, what are all this packages? Also, do I need to use Owin hosting? I got an 'Unable to find package Ninject.Web.WebApi' message from nuget console so I assume this one doesn't exist anymore?.

Thanks.

Ernesto
  • 1,523
  • 1
  • 14
  • 32

1 Answers1

9

There are several packages you need to get Ninject working with Web API 2:

  • Ninject (base library)
  • Ninject.Web.Common
  • Ninject.Web.Common.WebHost
  • Ninject.Web.WebApi.WebHost ("Ninject Web Host for WebApi 2")
  • Ninject.Web.WebApi ("Ninject Integration for WebApi 2")

After you install all of those, you'll find a pre-built NinjectWebCommon class in your App_Start folder. Just add you bindings to the RegisterServices() method.

Pharylon
  • 9,796
  • 3
  • 35
  • 59
  • Thanks Pharylon, so, it seems this answer http://stackoverflow.com/a/14018888/641530 is now outdated? We don't need to implement the custom dependency resolver anymore. What's the difference between the two web host packages? – Ernesto Jul 01 '14 at 14:54
  • One is simply the base Webhost package, and the other adds the WebApi 2 functionality. But, yes, that link is outdated now. I think Web Api 2 changed some core DI stuff, so there are new Ninject packages. – Pharylon Jul 01 '14 at 18:10
  • 1
    Yes, but I am more interested in knowing what they do, hosting for what? Do I need the common.webhost if I have the webapi.webhost? What if I use owin webhost? Do I still need the others? – Ernesto Jul 01 '14 at 21:22
  • After long time and installing it all and making it work (not knowing exactly what this packages do) I think it is fare to say this is the closest answer I will get. See also https://groups.google.com/forum/#!topic/ninject/HNhi7nv28-M The same question about the two webhosts. – Ernesto Sep 30 '15 at 13:22