将IOC容器与IdentityFactory中间件一起使用 [英] using IOC container with IdentityFactory Middleware
问题描述
我试图了解此处解释的UserManagerFactory中间件
I am trying to understand the UserManagerFactory middleware explained here per request lifetime management for usermanager.
我创建了这个类,该类是通过启动配置"方法调用的
I created this class which I am calling from the Startup Configuration method
public class CustomUserManagerProvider
{
public static CustomUserStore<CustomUser> CreateCustomUserStore()
{
return new CustomUserStore<CustomUser>(/*Need to inject dependencies here*/);
}
public static CustomUserManager CreateCustomUserManager(IdentityFactoryOptions<CustomUserManager> options,
IOwinContext context)
{
return new CustomUserManager(context.Get<CustomUserStore<CustomUser>>());
}
}
和启动配置
public void Configuration(IAppBuilder app)
{
app.CreatePerOwinContext(CustomUserManagerProvider.CreateCustomUserStore);
app.CreatePerOwinContext<IngramUserManager>(CustomUserManagerProvider.CreateCustomUserManager);
////....Other things
}
现在,我的CustomUserStore有一些依赖关系,我想在构造函数中注入这些依赖关系.
Now, My CustomUserStore has some dependencies which I want to inject in the constructor.
IOC容器的组合根知道如何解决这些依赖关系.
The composition root of the IOC container knows how to resolve these dependencies.
如何使CustomUserManagerProvider
DI容器知道(如果有意义)...
How do I make the CustomUserManagerProvider
DI container aware(If that makes sense)...
虽然有效
public static CustomUserStore<CustomUser> CreateCustomUserStore()
{
var dependency = DependencyResolver.Current.GetService<ISomeDependency>();
return new CustomUserStore<CustomUser>(dependency);
}
但是,我试图避免使用服务定位器(反)模式.这是我唯一的选择,这还对吗?
But, I was trying to avoid the service locator (anti)pattern. Is this my only option, Is this even right??
我正在使用Ninject.
I am using Ninject.
我不能在组合根目录的requestScope中创建一个UserManager并注入到控制器中,
Cant I just create a UserManager in requestScope in the composition root and inject into the controllers, wont that be the same thing?
在网络应用中,CreatePerOwinContext是否与创建InRequestScope相同?
In a web app, is CreatePerOwinContext same as creating InRequestScope?
推荐答案
是的,您可以根据需要将UserManager以不同的方式注入到控制器中,不需要使用CreatePerOwinContext/IdentityFactoryMiddleware.
Yes, you can inject the UserManager into your controllers a different way if you like, there's nothing that requires using the CreatePerOwinContext/IdentityFactoryMiddleware.
这篇关于将IOC容器与IdentityFactory中间件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!