title: Ninject and ASP.NET Web API
body: <p>Before I set up the question you should know that I got my current code from this page:
  <a href="http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/" rel="noreferrer">http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/</a></p>
  
  <p>I'm trying to use ASP.NET Web API and Ninject in my application by using an IDependencyResolver adapter found on the site above.</p>
  
  <p>I created all the code just like it shows on the site and it works but when I load up my appication my regular controllers fail and show this error:</p>
  
  <p><code>[MissingMethodException: No parameterless constructor defined for this object.]</code><br>
  <code>[InvalidOperationException: An error occurred when trying to create a controller of type 'AccountManager.Controllers.HomeController'...</code></p>
  
  <p>So, it seems like I can use Ninject with regular controllers or Web API controllers but not both. :(</p>
  
  <p>Here is my code:</p>
  
  <p><strong>NinjectResolver.cs</strong></p>
  
  <pre><code>public class NinjectResolver : NinjectScope, IDependencyResolver
  {
      private IKernel _kernel;
  
      public NinjectResolver(IKernel kernel)
          : base(kernel)
      {
          _kernel = kernel;
      }
  
      public IDependencyScope BeginScope()
      {
          return new NinjectScope(_kernel.BeginBlock());
      }
  }
  </code></pre>
  
  <p><strong>NinjectScope.cs</strong></p>
  
  <pre><code>public class NinjectScope : IDependencyScope
  {
      protected IResolutionRoot resolutionRoot;
  
      public NinjectScope(IResolutionRoot kernel)
      {
          resolutionRoot = kernel;
      }
  
      public object GetService(Type serviceType)
      {
          IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
          return resolutionRoot.Resolve(request).SingleOrDefault();
      }
  
      public IEnumerable&lt;object&gt; GetServices(Type serviceType)
      {
          IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
          return resolutionRoot.Resolve(request).ToList();
      }
  
      public void Dispose()
      {
          IDisposable disposable = (IDisposable)resolutionRoot;
          if (disposable != null) disposable.Dispose();
          resolutionRoot = null;
      }
  }
  </code></pre>
  
  <p><strong>Global.asax.cs</strong></p>
  
  <pre><code>public class MvcApplication : System.Web.HttpApplication
  {
      private void SetupDependencyInjection()
      {
          //create Ninject DI Kernel
          IKernel kernel = new StandardKernel();
  
          //register services with Ninject DI container
          RegisterServices(kernel);
  
          //tell asp.net mvc to use our Ninject DI Container
          GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);
      }
  }
  </code></pre>
  
  <p><strong>AccountingController.cs</strong></p>
  
  <pre><code>public class AccountingController : ApiController
  {
      private ICustomerService _customerService;
  
      public AccountingController(ICustomerService service)
      {
          _customerService = service;
      }
  
      // GET /api/&lt;controller&gt;/5
      public string Get(int id)
      {
          return "value";
      }
  }
  </code></pre>