c# - SimpleInjector doesn't work - Web API on OWIN -
owinstartup.cs
public class owinstartup { internal static idataprotectionprovider dataprotectionprovider { get; private set; } public void configuration(iappbuilder app) { dataprotectionprovider = app.getdataprotectionprovider(); var config = new httpconfiguration(); simpleinjectorconfig.configure(app); configureoauth(app); webapiconfig.register(config); app.usecors(corsoptions.allowall); app.usewebapi(config); } private static void configureoauth(iappbuilder app) { app.createperowincontext( () => (idisposable)globalconfiguration.configuration.dependencyresolver.getservice( typeof(appusermanager))); var options = new oauthauthorizationserveroptions { tokenendpointpath = new pathstring("/token"), provider = new appauthprovider(), allowinsecurehttp = true, }; app.useoauthauthorizationserver(options); app.useoauthbearerauthentication(new oauthbearerauthenticationoptions()); } }
simpleinjectorconfig.cs
public static class simpleinjectorconfig { public static void configure(iappbuilder app) { var container = new container(); container.options.defaultscopedlifestyle = new asyncscopedlifestyle(); //allows scoped instances resolved during owin request app.use(async (context, next) => { using (asyncscopedlifestyle.beginscope(container)) { await next(); } }); container.register<appidentitydbcontext>(lifestyle.scoped); container.register<appusermanager>(lifestyle.scoped); container.register( () => container.isverifying ? new owincontext().authentication : httpcontext.current.getowincontext().authentication, lifestyle.scoped); container.register<appsigninmanager>(lifestyle.scoped); container.verify(); globalconfiguration.configuration.dependencyresolver = new simpleinjectorwebapidependencyresolver(container); } }
so in implemenation of oauthauthorizationserverprovider
called appauthprovider
im trying instance of appusermanager
( need find user ) using code:
var manager = context.owincontext.get<appusermanager>();
but dont know why still null
. dont know because everythings seems configured correctly. ideas ? !
i found solution. updated code below:
owinstartup.cs
public class owinstartup { internal static idataprotectionprovider dataprotectionprovider { get; private set; } public void configuration(iappbuilder app) { dataprotectionprovider = app.getdataprotectionprovider(); var container = simpleinjectorconfig.configure(); //allows scoped instances resolved during owin request app.use(async (context, next) => { using (asyncscopedlifestyle.beginscope(container)) { await next(); } }); var config = new httpconfiguration { dependencyresolver = new simpleinjectorwebapidependencyresolver(container) }; configureoauth(app, config); webapiconfig.register(config); app.usecors(corsoptions.allowall); app.usewebapi(config); } private static void configureoauth(iappbuilder app, httpconfiguration config) { app.createperowincontext( () => (appusermanager)config.dependencyresolver.getservice( typeof(appusermanager))); var options = new oauthauthorizationserveroptions { tokenendpointpath = new pathstring("/token"), provider = new appauthprovider(), //todo: zmienic przy wyjsciu w live. allowinsecurehttp = true, }; app.useoauthauthorizationserver(options); app.useoauthbearerauthentication(new oauthbearerauthenticationoptions()); } }
simpleinjectorconfig.cs
public static class simpleinjectorconfig { public static container configure() { var container = new container(); container.options.defaultscopedlifestyle = new asyncscopedlifestyle(); container.register<appidentitydbcontext>(lifestyle.scoped); container.register<appusermanager>(lifestyle.scoped); container.register( () => container.isverifying ? new owincontext().authentication : httpcontext.current.getowincontext().authentication, lifestyle.scoped); container.register<appsigninmanager>(lifestyle.scoped); container.verify(); return container; } }
maybe use it.
Comments
Post a Comment