I have a Web Api 2 App, with two classes that both depend on another class, and i'm using ninject to resolve the dependancies.
public class AuthorizationServerProvider : OAuthAuthorizationServerProvider
{
private IUserService _userService;
public AuthorizationServerProvider(IUserService userService)
{
_userService = userService;
}
}
public class RefreshTokenProvider : IAuthenticationTokenProvider
{
private IUserService _userService;
public RefreshTokenProvider(IUserService userService)
{
_userService = userService;
}
In the startup.cs class i need to use the above two classes, but of course i cannot use constructor injection in the startup class, as that is initialised before Ninject is.
What's a way around this so that the references to _tokenProvider and _authServerProvider in the ConfigureAuth method?
public class Startup
{
private AuthorizationServerProvider _authServerProvider;
private RefreshTokenProvider _tokenProvider;
public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; private set; }
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
app.UseNinjectMiddleware(CreateKernel);
app.UseNinjectWebApi(config);
ConfigureOAuth(app);
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
public void ConfigureOAuth(IAppBuilder app)
{
var oAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true, //TODO: HTTPS
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
Provider = _authServerProvider,
RefreshTokenProvider = _tokenProvider
};
}
Here's the CreateKernel Method
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
And here's where I register my services
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<SimpleAuthorizationServerProvider>().ToSelf();
kernel.Bind<SimpleRefreshTokenProvider>().ToSelf();
}
I've followed the advice in the ninject docs, but to no avail.