title: How to use DI container when OwinStartup
body: <p>It's a Web API 2 project.</p>
  
  <p>When I implement DI using Ninject, I got an error message</p>
  
  <p>An error occurred when trying to create a controller of type 'TokenController'. Make sure that the controller has a parameterless public constructor.</p>
  
  <pre><code>[assembly: OwinStartup(typeof(Web.Startup))]
  
  namespace Web
  {
      public partial class Startup
      {
          public void Configuration(IAppBuilder app)
          {
              ConfigureAuth(app);
              ConfigureWebApi(app);
          }
      }
  }
  
  public class TokenController : ApiController
  {
  
      private IUserService _userService;
  
      public TokenController(IUserService userService)
      {
          this._userService = userService;
      }
  
      [Route("api/Token")]
      public HttpResponseMessage PostToken(UserViewModel model)
      {
          if (_userService.ValidateUser(model.Account, model.Password))
          {
              ClaimsIdentity identity = new ClaimsIdentity(Startup.OAuthBearerOptions.AuthenticationType);
              identity.AddClaim(new Claim(ClaimTypes.Name, model.Account));
              AuthenticationTicket ticket = new AuthenticationTicket(identity, new AuthenticationProperties());
              var currentUtc = new SystemClock().UtcNow;
              ticket.Properties.IssuedUtc = currentUtc;
              ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(30));
              return new HttpResponseMessage(HttpStatusCode.OK)
              {
                  Content = new ObjectContent&lt;object&gt;(new
                  {
                      UserName = model.Account,
                      AccessToken = Startup.OAuthBearerOptions.AccessTokenFormat.Protect(ticket)
                  }, Configuration.Formatters.JsonFormatter)
              };
          }
  
          return new HttpResponseMessage(HttpStatusCode.BadRequest);
      }
  }
  </code></pre>
  
  <p>When I add <code>&lt;add key="owin:AutomaticAppStartup" value="false" /&gt;</code> to web.config</p>
  
  <p>Ninject works fine, 
  but <strong>Startup.OAuthBearerOptions.AccessTokenFormat</strong> becomes to null</p>
  
  <p>How to use DI container with OWIN?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Implement IDependencyResolver and use the WebAPI Dependency Resolver as below</p>
  
  <pre><code>public void ConfigureWebApi(IAppBuilder app)
  {
      HttpConfiguration config = new HttpConfiguration();
  
      config.DependencyResolver = new NinjectDependencyResolver(NinjectWebCommon.CreateKernel());
  
      app.UseWebApi(config);
  }
  </code></pre>
  
  <p><a href="http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api">NinjectDependencyResolver</a></p>
  
  <hr>
  
  <p>In Simple Injector case</p>
  
  <pre><code>public void ConfigureWebApi(IAppBuilder app)
  {
      HttpConfiguration config = new HttpConfiguration();
  
      var container = new Container();
      container.Register&lt;IUserService, UserService&gt;();
      config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
  
      app.UseWebApi(config);
  }
  </code></pre>
  
  <p><a href="http://simpleinjector.codeplex.com/wikipage?title=Web%20API%20Integration">SimpleInjectorWebApiDependencyResolver</a></p>