2017-04-07 81 views
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。我真的不知道該怎麼做,因爲所有東西似乎配置正確。有任何想法嗎 ?謝謝 !

+0

[在Web API和OWIN中使用簡單注入器]可能的重複(http://stackoverflow.com/questions/28230951/using-simple-injector-in-web-api-and-owin) – Martin

+0

這可以幫助我,但我仍然在'空'。投到「AppUserManager」而不是「IDisposable」可以解決問題。 – Bardr

回答

0

我找到了解決方案。下面更新代碼:

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 to be 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; 
    } 
} 

也許有人會使用它。