title: Ninject not working with Asp.Net WebApi
body: <p>I have seen so many people ask this question and have tried what they accepted as answers but none of them have worked for me.</p>
  
  <p>I keep getting this error when I try to use Ninject with my asp.net webapi</p>
  
  <pre><code> { "Message": "An error has occurred.", "ExceptionMessage": "An error
   occurred when trying to create a controller of type
   'SubjectPostsController'. Make sure that the controller has a
   parameterless public constructor.", "ExceptionType":
  "System.InvalidOperationException", "StackTrace": " at
  System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create
  (HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type
  controllerType)\ \ at
  System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage
  request)\ \ at System.Web.Http.Dispatcher.HttpControllerDispatcher.&lt;SendAsync&gt;d__1.MoveNext ()",
  "InnerException": { "Message": "An error has occurred.",
  "ExceptionMessage": "Type
  'Analyzer.Controllers.SubjectPostsController' does not have a default
   constructor", "ExceptionType": "System.ArgumentException",
  "StackTrace": " at System.Linq.Expressions.Expression.New(Type type)\
  \ at System.Web.Http.Internal.TypeActivator.Create[TBase](Type
  instanceType)\ \ atSystem.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage
  request, Type controllerType, Func`1&amp; activator)\ \ at  System.Web.Http.Dispatcher.DefaultHttpControllerActivator.CreateHttpRequestMessa ge
  request, HttpControllerDescriptor controllerDescriptor, Type
  controllerType)" } }
  </code></pre>
  
  <p>My Controller:</p>
  
  <pre><code>using Analyzer.Models.ModelsForApp;
  using Analyzer.Repository;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Net.Http;
  using System.Web.Http;
  
  namespace Analyzer.Controllers
  {
      public class SubjectPostsController : ApiController
      {
          private IRepository _repo;
          public SubjectPostsController(IRepository repo)
          {
              _repo = repo;
          }
          public IEnumerable&lt;SubjectPost&gt; Get()
          {
              var posts = _repo.GetSubjectPosts()
             .OrderByDescending(p =&gt;p.Created).ToList();
              return posts;
  
          }
      }
  }
  </code></pre>
  
  <p>The Ninject.WebCommon:</p>
  
  <pre><code>[assembly: WebActivatorEx.PreApplicationStartMethod(typeof  Analyzer.App_Start.NinjectWebCommon), "Start")]
  [assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof (Analyzer.App_Start.NinjectWebCommon), "Stop")]
  
  namespace Analyzer.App_Start
  {
      using System;
      using System.Web;
  
  using Microsoft.Web.Infrastructure.DynamicModuleHelper;
  
  using Ninject;
  using Ninject.Web.Common;
  using Analyzer.Database;
  using Analyzer.Repository;
  using System.Web.Http;
  
  
  public static class NinjectWebCommon 
  {
      private static readonly Bootstrapper bootstrapper = new Bootstrapper();
  
      /// &lt;summary&gt;
      /// Starts the application
      /// &lt;/summary&gt;
      public static void Start() 
      {
          DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
          DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
          bootstrapper.Initialize(CreateKernel);
      }
  
      /// &lt;summary&gt;
      /// Stops the application.
      /// &lt;/summary&gt;
      public static void Stop()
      {
          bootstrapper.ShutDown();
      }
  
      /// &lt;summary&gt;
      /// Creates the kernel that will manage your application.
      /// &lt;/summary&gt;
      /// &lt;returns&gt;The created kernel.&lt;/returns&gt;
      private static IKernel CreateKernel()
      {
          var kernel = new StandardKernel();
          try
          {
              kernel.Bind&lt;Func&lt;IKernel&gt;&gt;().ToMethod(ctx =&gt; () =&gt; new Bootstrapper().Kernel);
              kernel.Bind&lt;IHttpModule&gt;().To&lt;HttpApplicationInitializationHttpModule&gt;();
  
              RegisterServices(kernel);
              return kernel;
          }
          catch
          {
              kernel.Dispose();
              throw;
          }
      }
  
      /// &lt;summary&gt;
      /// Load your modules or register your services here!
      /// &lt;/summary&gt;
      /// &lt;param name="kernel"&gt;The kernel.&lt;/param&gt;
      private static void RegisterServices(IKernel kernel)
      {
          kernel.Bind&lt;ApplicationDbContext&gt;().To&lt;ApplicationDbContext&gt;().InRequestScope();
          kernel.Bind&lt;IRepository&gt;().To&lt;Repository&gt;().InRequestScope();
      }        
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The nuget packages I have installed are:</p>
  
  <pre><code>Ninject version 3.2.2.0
  Ninject integration for WepApi2 3.2.4.0
  Ninject Web Host for WebApi2 3.2.4.0
  Ninject.MVC3 3.2.1.0
  Ninject.Web 3.2.1.0
  Ninject.Web.Common 3.2.3.0
  Ninject.Web.Common.WebHost 3.2.3.0
  WebApiContrib.IoC.Ninject
  </code></pre>
  
  <p>So far I've tried so many possibilities,
  The GlobalConfiguration.Configuration.DependencyResolver throws an exception when I tried that method.</p>
  
  <p>So basically, I've run out of ideas. Does anyone know how to solve this?</p>
  
  <p>UPDATE: I tried solving this using Unity and it worked. Although I don't know the drawbacks of using unity and the amount of support it gets(It doesn't seem to have been updated for almost a year). Researching more on unity but I am still interested in a solution using Ninject.</p>
  
  <p>UPDATE 2: It also works with SimpleInjector...I'm beginning to think the problem is from Ninject. Can anyone get Ninject working with WebApi?</p>