title: Asp.Net API 2 Ninject and OWIN OAuth Configuration
body: <p>I know there are many posts very similar to this one. However, I have been through them and have not been able to find a solution to my problem.</p>
  
  <p>I am implementing OAth authentication using OWIN into a previous project which utilises Ninject for dependency injection. </p>
  
  <p>To implement OATH using OWIN switched from the original Global.asax Application_Start configuration method to creating a Start.cs file/class which is used to configure OWIN.</p>
  
  <p>The problem I am facing is that no matter what implementation I try upon running the code, an API request always returns the error "Make sure that the controller has a parameterless public constructor".</p>
  
  <p>Please can some one shed some light on why neither of the below approaches work?</p>
  
  <p>My first attempt at resolving the issue was to use app.UseNinjectMiddleware with app.UseNinjectWebApi. Secondly I tried a NinjectDependencyResolver which has been mentioned on many previous posts. <a href="https://stackoverflow.com/questions/19781970/how-to-use-di-container-when-owinstartup">Link to similar post</a>.</p>
  
  <p>Below is the code contining both implementations.</p>
  
  <pre><code>public class Startup
  {
      public void Configuration( IAppBuilder app )
      {
          var config = new HttpConfiguration();
          var kernel = NinjectWebCommon.CreateKernel();
  
          // Using this and UseNinjectWebAPI
          app.UseNinjectMiddleware( () =&gt; kernel );
  
          ConfigureOAuthTokenGeneration( app );
          ConfigureWebApi( config );
  
          app.UseCors( Microsoft.Owin.Cors.CorsOptions.AllowAll );
  
          WebApiConfig.Register( config );
          app.UseNinjectWebApi( config );
  
          // Also tried this ( when the following lines are uncommented UseNinjectMiddleware and useNinjectWebApi is commented )
          // This causes "Make sure that the controller has a parameterless public constructor" error
          //GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver( kernel );
          //app.UseWebApi( config );
      }
  
      private void ConfigureOAuthTokenGeneration( IAppBuilder app )
      {
          // Configure the db context and user manager to use a single instance per request
          app.CreatePerOwinContext( ApplicationDbContext.Create );
          app.CreatePerOwinContext&lt;ApplicationUserManager&gt;( ApplicationUserManager.Create );
  
          // Plugin the OAuth bearer JSON Web Token tokens generation and Consumption will be here
  
      }
  
      private void ConfigureWebApi( HttpConfiguration config )
      {
          var jsonFormatter = config.Formatters.OfType&lt;JsonMediaTypeFormatter&gt;().First();
          jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
      }
  }
  </code></pre>
  
  <p>Here is my NinjectWebCommon class</p>
  
  <pre><code>//[assembly:     WebActivatorEx.PreApplicationStartMethod(typeof(TimeUp.Web.Api.App_Start.NinjectWebCommon), "Start")]
  //[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(TimeUp.Web.Api.App_Start.NinjectWebCommon), "Stop")]
  
  namespace Shopping
  {
      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;
          public 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 )
          {
          }
      }
  }
  </code></pre>
  
  <p>Thanks in advanced</p>