How to use DI container when OwinStartup
You might want to take a look at this blog post.
It's using Unity but it should end-up being the same.
Basically, use the WebAPI Dependency Resolver. Make sure that everything is mapped properly and it should be fine.
If after setting up your DI you still have problem with your OAuth token, let me know.
Cheers
Update
This is now more straight forward thanks to the Nuget package Ninject.Web.WebApi.OwinHost:
Startup.cs
using Ninject;
using Ninject.Web.Common.OwinHost;
using Ninject.Web.WebApi.OwinHost;
using Owin;
using System.Web.Http;
namespace Xxx
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute("DefaultApi", "myservice/{controller}/{id}", new { id = RouteParameter.Optional });
app.UseNinjectMiddleware(CreateKernel);
app.UseNinjectWebApi(config);
}
}
public static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<IMyService>().To<MyService>();
return kernel;
}
}
I have updated the wiki accordingly.
https://github.com/ninject/Ninject.Web.Common/wiki/Setting-up-a-OWIN-WebApi-application
All three hosting options.
https://github.com/ninject/Ninject.Web.WebApi/wiki/Setting-up-an-mvc-webapi-application
We use the standard ninject.MVC5 package installed with nuget
PM> install-package ninject.MVC5
Then we configure our bindings like so.
kernel.Bind<IDbContext, DbContext>()
.To<BlogContext>()
.InRequestScope();
kernel.Bind<IUserStore<User>>()
.To<UserStore<User>>()
.InRequestScope();
kernel.Bind<IDataProtectionProvider>()
.To<DpapiDataProtectionProvider>()
.InRequestScope()
.WithConstructorArgument("ApplicationName");
kernel.Bind<ApplicationUserManager>().ToSelf().InRequestScope()
.WithPropertyValue("UserTokenProvider",
new DataProtectorTokenProvider<User>(
kernel.Get<IDataProtectionProvider>().Create("EmailConfirmation")
));
You may need to adjust dependent on how much you have customized your user model. For instance the user store binding may be something like.
kernel.Bind<IUserStore<User, int>>()
.To<IntUserStore>().InRequestScope();
Also any setting up of the user manger you require i.e. password policies can be set in your user manager constructor.
Previously this could be found in the create method in the sample, you will no longer require this. also you can now get rid of the owin get context calls as ninject will handle resolution for you.