2017-05-12 12 views
0

DI를 OWIN CreatePerOwinContext 확장자로 사용하려고합니다. 나는 OAuthAuthorizationServerProvider도 사용하고 있습니다. OAuthAuthorizationServerProvider 내부에서 다음을 사용하여 내 사용자 관리자의 인스턴스를 가져 오려고합니다. OAuthGrantResourceOwnerCredentialsContext.OwinContext.GetUserManager.CreatePerOwinContext와 함께 Dependency Injection을 사용하려고 할 때 ObjectDisposedException

시작 UP 파일 :

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
     { 
      //Inspecting the _userManager I see the ObjectDisposedException 
      _userManager = context.OwinContext.GetUserManager<AppUserManager>(); 


      var user = await _userManager.FindByNameAsync(context.UserName); 
     } 
: 나는 AppUserManager의 인스턴스를 얻으려고 내 OAuthAuthorizationServerProvider 구현의 GrantResourceOwnerCredentials 내부

private static readonly Lazy<IUnityContainer> Container = new 

    public static IUnityContainer GetConfiguredContainer() 
    { 
     return Container.Value; 
    } 

    Lazy<IUnityContainer>(() => { 
       var container = new UnityContainer(); 

       //Registers the Types 
       Register(container); 

       return container; 
      }); 

:

public void Configuration(IAppBuilder app) 
     { 
      DataProtectionProvider = app.GetDataProtectionProvider(); 
      var config = new HttpConfiguration {DependencyResolver = new UnityDependencyResolver(UnityRegistrations.GetConfiguredContainer())}; 
      WebApiConfig.Register(config); 

      //Allow Cross Domain Calls 
      app.UseCors(CorsOptions.AllowAll); 



      //I verified that my AppUserManager is getting constructed properly 
      //var manager = UnityRegistrations.GetConfiguredContainer().Resolve<AppUserManager>(); 

      app.CreatePerOwinContext(() => UnityRegistrations.GetConfiguredContainer().Resolve<AppUserManager>()); 


      OAuthOptions = new OAuthAuthorizationServerOptions 
      { 
       // Point at which the Bearer token middleware will be mounted 
       TokenEndpointPath = new PathString("/token"), 
       // An implementation of the OAuthAuthorizationServerProvider which the middleware 
       // will use for determining whether a user should be authenticated or not 
       Provider = new OAuthProvider("self"), 
       // How long a bearer token should be valid for 
       AccessTokenExpireTimeSpan = TimeSpan.FromHours(24), 
       // Allows authentication over HTTP instead of forcing HTTPS 
       AllowInsecureHttp = true 
      }; 

      app.UseOAuthBearerTokens(OAuthOptions); 
      app.UseWebApi(config); 
     } 

이것은 GetConfiguredContainer 방법

내가 무엇입니까? 웹 API와 Owin으로도 가능한 일을하려고합니까?

답변

0

나는 신참 실수를 저질 렀습니다. 내 AppUserManager Unity 등록에 대한 어떤 이유로 인해 HierarchicalLifetimeManager을 추가했습니다. 이것은 분명히 실수였습니다. 그것은 조기에 처리하고있었습니다. 내 DbContext도 등록시 HierarchicalLifetimeManager입니다. 즐거움의 시간!

잘못

_unityContainer.RegisterType<AppUserManager>(new HierarchicalLifetimeManager()); 

올바른

_unityContainer.RegisterType<AppUserManager>();