Home
Atom Feed

Using Ninject with WebApi RC

Since WebApi came out in Beta release, I've used it on several less-than-production-critical systems. My IoC container of choice has always been Ninject, and getting it to work with WebApi has been relatively straightforward by using the Ninject.Web.WebApi package.

With the recent release of the Release Candidate, the old method does not work anymore. There are some guides on how to make it work, but they have flaws. I've made a fork and fixed it, but the main project has yet to be updated.

UPDATE: The fork is now available as a NuGet package to make installation easier.

If you want to use Ninject with WebApi RC, you should do the following:

  1. Install the Microsoft.AspNet.WebApi NuGet package
  2. Install the Ninject.Web.Common NuGet package
  3. Download and build my Ninject.Web.WebApi fork and reference the resulting dll

After this, load your modules / bind your dependencies in the RegisterServices function in the App_Start\NinjectWebCommon.cs file.

Then, use constructor injection or your method of choice to get your classes.
You can also get an instance of a dependency by using the GlobalConfiguration.Configuration.DependencyResolver class (which by doing the above is Ninject).