Quick tip: How to Make Ninject work with ASP.NET Web API

Dependency Injection is something which comes along with almost every new project we are building nowadays. I've recently used Web API several times from it's alpha version and now to the latest versions... With each new version, there were some updates to Ninject in order to make it work properly and be easily configurable with Web API mechanism. It's very natural to use Dependency Injection with Web API as it's also based on Controllers same as in any ASP.NET MVC application.

The latest Web Api version could be installed directly from NuGet by running the following command:

PM> Install-Package AspNetWebApi

The current version I've installed is v4.0.20710.0

Previously, there were bunch of specific Ninject NuGet packages dedicated for WebAPI, however these were made for the primary alpha versions, thus the current package you only need to install is the following:

PM> Install-Package Ninject.WebApi.DependencyResolver

This package will install all it's dependent packages:

Log

PM> Install-Package Ninject.WebApi.DependencyResolver
Attempting to resolve dependency 'Ninject.Web.Common (≥ 3.0.0.7)'.
Attempting to resolve dependency 'Ninject (≥ 3.0.0.0 && < 3.1.0.0)'.
Attempting to resolve dependency 'WebActivator (≥ 1.5)'.
Attempting to resolve dependency 'Microsoft.Web.Infrastructure (≥ 1.0.0.0)'

Once installed, you will get NinjectWebCommon.cs inside App_Start. Open that file and add the following line inside CreateKernel() method right after RegisterServices(kernel);

System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);

Now, you can freely create your dependency injection binding maps and take advantage of DI mechanisms. The way you will use it exactly the same as in ASP.NET MVC... There are many articles and tutorials you can search on web for that.

Happy Coding!

Hajan
Published Saturday, March 16, 2013 5:22 PM by hajan

Comments

# re: Quick tip: How to Make Ninject work with ASP.NET Web API

Tuesday, March 19, 2013 8:52 AM by aresear

Thanks for sharing this info!!!

# re: Quick tip: How to Make Ninject work with ASP.NET Web API

Wednesday, March 20, 2013 11:46 PM by pankaj.nikam

I love Ninject. I had used it in MVC... Its nice to know how to make it available for WebAPI. Thanks for the article :)

# re: Quick tip: How to Make Ninject work with ASP.NET Web API

Thursday, September 19, 2013 2:40 AM by Stephen Cossgrove

Hi,

I followed your example and got ninject working really quick, Thanks.

But when I added the Microsoft.AspNet.WebApi.Cors package it now breaks at the setting the resolvers line in the CreateKernel.

Do you know if this is an known issue?

# re: Quick tip: How to Make Ninject work with ASP.NET Web API

Thursday, September 26, 2013 8:48 PM by Rasshme

cool, this works !!!  thanks for this :)

# re: Quick tip: How to Make Ninject work with ASP.NET Web API

Wednesday, December 4, 2013 7:47 AM by Raza

This still breaks when CORS is enabled. Any solution to that?

Thanks for sharing your feedback! If your feedback doesn't appear right away, please be patient as it may take a few minutes to publish - or longer if the blogger is moderating comments.

Leave a Comment

(required) 
(required) 
(optional)
(required)