title: InvalidOperationException when injecting into API controllers with Ninject and Web API 2.1
body: <p>I'm currently having trouble getting Ninject to inject dependencies into my API controllers. I am using a Web project with MVC 5, Web API 2.1 and SignalR 2. I'm using version 5.1.2 of Web API, version 3.2.2 of Ninject, and version 3.2.1 of Ninject.Web.WebApi.Webhost. Every time I make a request against the API, I get this error:</p>
  
  <pre><code>    System.InvalidOperationException: An error occurred when trying to create a controller of type 'AccountsController'. Make sure that the controller has a parameterless public constructor. ---&gt; System.NullReferenceException: Object reference not set to an instance of an object.
     at Ninject.Planning.Bindings.BindingConfiguration.GetProvider(IContext context)
     at Ninject.Planning.Bindings.Binding.GetProvider(IContext context)
     at Ninject.Activation.Context.GetProvider()
     at Ninject.Activation.Context.ResolveInternal(Object scope)
     at Ninject.Activation.Context.Resolve()
     at Ninject.KernelBase.&lt;&gt;c__DisplayClass15.&lt;Resolve&gt;b__f(IBinding binding)
     at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
     at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
     at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
     at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
     at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
     at Ninject.Activation.Providers.StandardProvider.&lt;&gt;c__DisplayClass4.&lt;Create&gt;b__2(ITarget target)
     at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
     at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
     at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
     at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
     at Ninject.Activation.Context.ResolveInternal(Object scope)
     at Ninject.Activation.Context.Resolve()
     at Ninject.KernelBase.&lt;&gt;c__DisplayClass15.&lt;Resolve&gt;b__f(IBinding binding)
     at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
     at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
     at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
     at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
     at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
     at Ninject.Activation.Providers.StandardProvider.&lt;&gt;c__DisplayClass4.&lt;Create&gt;b__2(ITarget target)
     at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
     at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
     at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
     at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
     at Ninject.Activation.Context.ResolveInternal(Object scope)
     at Ninject.Activation.Context.Resolve()
     at Ninject.KernelBase.&lt;&gt;c__DisplayClass15.&lt;Resolve&gt;b__f(IBinding binding)
     at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
     at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
     at Ninject.Web.WebApi.NinjectDependencyScope.GetService(Type serviceType)
     at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1&amp; activator)
     at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
     --- End of inner exception stack trace ---
     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.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
     at System.Web.Http.Dispatcher.HttpControllerDispatcher.&lt;SendAsync&gt;d__0.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Web.Http.Dispatcher.HttpControllerDispatcher.&lt;SendAsync&gt;d__0.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at System.Web.Http.HttpServer.&lt;SendAsync&gt;d__0.MoveNext()
  </code></pre>
  
  <p>Here is the controller it is trying to instantiate:</p>
  
  <pre><code>public class AccountsController : ApiControllerBase
  {
      private IAuthenticationService _auth;
      private IConfigurationManager _config;
      private static readonly ILog LOG = LogManager.GetLogger(typeof(AccountsController));
  
      public AccountsController(IAuthorizationService authService, IAuthenticationService auth, IConfigurationManager config)
          : base(authService)
      {
          _auth = auth;
          _config = config;
      }
  </code></pre>
  
  <p>And here is my <code>Startup.cs</code> file:</p>
  
  <p>public class Startup
      {
          public static IKernel Kernel { get; private set; }</p>
  
  <pre><code>    static Startup()
      {
          Kernel = new StandardKernel();
          RegisterServices(Kernel);
      }
      public void Configuration(IAppBuilder app)
      {
          // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
  
  
  
          var webApiConfig = ConfigureWebApi(Kernel);
  
          app.UseWebApi(webApiConfig);
      }
  
      public static HttpConfiguration ConfigureWebApi(IKernel kernel)
      {
          var config = new HttpConfiguration();
          config.DependencyResolver = new Ninject.Web.WebApi.NinjectDependencyResolver(kernel);
  
          //Formatting
          config.Formatters.Remove(config.Formatters.XmlFormatter);
          config.Formatters.Remove(config.Formatters.JsonFormatter);
          config.Formatters.Add(new JsonMediaTypeFormatter()
          {
              SerializerSettings = JsonSettings.SETTINGS,
              Indent = true,
          });
  
          //Error handling
          config.Services.Add(typeof(IExceptionLogger), new Log4NetExceptionLogger());
          config.Services.Replace(typeof(IExceptionHandler), new EntityErrorExceptionHandler());
  
          //Routing
          config.MapHttpAttributeRoutes();
          config.EnsureInitialized();
          return config;
      }
  
  
      private static void RegisterServices(IKernel kernel)
      {
          kernel.Load&lt;ConfigurationModule&gt;();
          kernel.Load&lt;ResourceModule&gt;();
          kernel.Load&lt;SecurityModule&gt;();
  
          kernel.Bind&lt;IEmailService&gt;().To&lt;Email.SendGridService&gt;();
  
  
      }
  }
  </code></pre>
  
  <p>Those modules contain all the necessary dependencies. I have verified that they are all getting bound properly. Does anyone know why I still get this error? Is there anything that can be done to fix it?</p>