0
OwinStartup.csSimpleInjector不工作 - 在OWIN的Web API
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 to be 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);
}
}
所以在我的OAuthAuthorizationServerProvider
所謂實行進出口AppAuthProvider
試圖讓AppUserManager
實例(我需要找到用戶)使用此代碼:
var manager = context.OwinContext.Get<AppUserManager>();
但不知道爲什麼我仍然得到null
。我真的不知道該怎麼做,因爲所有東西似乎配置正確。有任何想法嗎 ?謝謝 !
[在Web API和OWIN中使用簡單注入器]可能的重複(http://stackoverflow.com/questions/28230951/using-simple-injector-in-web-api-and-owin) – Martin
這可以幫助我,但我仍然在'空'。投到「AppUserManager」而不是「IDisposable」可以解決問題。 – Bardr