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 내 된 구현에 따라서
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 모르겠다. 나는 everythings가 정확하게 형성되는 것처럼 보이기 때문에 무엇을 해야할지 잘 모릅니다. 어떤 아이디어? 감사 !

+0

[웹 API와 OWIN의 간단한 인젝터 사용] 가능한 중복 (http://stackoverflow.com/questions/28230951/using-simple-injector-in-web-api-and-owin) – Martin

+0

이것은 나는 아직도'null '을 얻고 있었다. '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; 
    } 
} 

어쩌면 누군가가 그것을 사용합니다.