尝试将依赖注入与 CreatePerOwinContext 结合使用时出现 ObjectDisposedException
ObjectDisposedException when trying to use Dependency Injection with CreatePerOwinContext
我正在尝试将 DI 与 OWIN CreatePerOwinContext
扩展一起使用。我也在用OAuthAuthorizationServerProvider
。在 OAuthAuthorizationServerProvider 中,我正在尝试使用以下方法获取用户管理器的实例:OAuthGrantResourceOwnerCredentialsContext.OwinContext.GetUserManager.
启动文件:
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
方法:
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;
});
在我的 OAuthAuthorizationServerProvider
实现的 GrantResourceOwnerCredentials
中,我尝试获取 AppUserManager
:
的实例
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);
}
我正在尝试使用 Web API 和 Owin 做的事情是否可行?
我犯了一个新手错误。出于某种原因,我在 AppUserManager
Unity 注册中添加了一个 HierarchicalLifetimeManager
。显然,这是一个错误。它过早地处理了。我的 DbContext 在注册时也有一个 HierarchicalLifetimeManager
。欢乐时光!
错误
_unityContainer.RegisterType<AppUserManager>(new HierarchicalLifetimeManager());
正确
_unityContainer.RegisterType<AppUserManager>();
我正在尝试将 DI 与 OWIN CreatePerOwinContext
扩展一起使用。我也在用OAuthAuthorizationServerProvider
。在 OAuthAuthorizationServerProvider 中,我正在尝试使用以下方法获取用户管理器的实例:OAuthGrantResourceOwnerCredentialsContext.OwinContext.GetUserManager.
启动文件:
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
方法:
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;
});
在我的 OAuthAuthorizationServerProvider
实现的 GrantResourceOwnerCredentials
中,我尝试获取 AppUserManager
:
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);
}
我正在尝试使用 Web API 和 Owin 做的事情是否可行?
我犯了一个新手错误。出于某种原因,我在 AppUserManager
Unity 注册中添加了一个 HierarchicalLifetimeManager
。显然,这是一个错误。它过早地处理了。我的 DbContext 在注册时也有一个 HierarchicalLifetimeManager
。欢乐时光!
错误
_unityContainer.RegisterType<AppUserManager>(new HierarchicalLifetimeManager());
正确
_unityContainer.RegisterType<AppUserManager>();