0

OwinStartup.csSimpleInjectorが動作しない - ウェブAPIをOWINに

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

AppUserManagerのインスタンスを取得しようとしている AppAuthProviderイムと呼ばれる OAuthAuthorizationServerProviderの私のimplemenationでそう
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); 
    } 
} 

(Iユーザーを見つける必要があります)

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

私はまだ「ヌル」になっていた。 'IDisposable'の代わりに' AppUserManager'にキャストすると問題が解決します。 – 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; 
    } 
} 

たぶん誰かがそれを使用します。

関連する問題