I was using Ninject as a DI for an ASP.NET MVC project. Once you add your WebApi and try to use the DI you surprisingly (me) see its not working. So here is what I did to use Ninject for my API as well as MVC project.
- Install Nuget package
- You must already have all the packages you need for Ninject but for Web Api 2 there is another package to install called Ninject.Web.WebApi
- Config NinjectWebCommon.cs
- in CreateKernel method add GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel); before return kernel;
- use and Enjoy
- You are good to go and use your Ninject for Api as you were using for MVC. For sure if you need to add new bindings you are can add them in your AddBindings in NinjectDependencyResolver class
PS: Usually you might have a separate project for your API but I had to use MVC and API both in a same project for my own reasons.
Happy codding…