使用带有IdentityFactory中间件的IOC容器
我试图理解UserManagerFactory中间件,这里解释了usermanager的每个请求生命周期管理 。
我创建了这个我从Startup Configuration方法调用的类
public class CustomUserManagerProvider { public static CustomUserStore CreateCustomUserStore() { return new CustomUserStore(/*Need to inject dependencies here*/); } public static CustomUserManager CreateCustomUserManager(IdentityFactoryOptions options, IOwinContext context) { return new CustomUserManager(context.Get<CustomUserStore>()); } }
和启动配置
public void Configuration(IAppBuilder app) { app.CreatePerOwinContext(CustomUserManagerProvider.CreateCustomUserStore); app.CreatePerOwinContext(CustomUserManagerProvider.CreateCustomUserManager); ////....Other things }
现在,我的CustomUserStore有一些依赖项,我想在构造函数中注入。
IOC容器的组合根知道如何解决这些依赖关系。
如何CustomUserManagerProvider
DI容器(如果有意义的话)……
虽然这很有效
public static CustomUserStore CreateCustomUserStore() { var dependency = DependencyResolver.Current.GetService(); return new CustomUserStore(dependency); }
但是,我试图避免服务定位器(反)模式。 这是我唯一的选择吗,这是对的吗?
我正在使用Ninject。
我只是在组合根中的requestScope中创建一个UserManager并注入到控制器中,这不是一回事吗?
在Web应用程序中,CreatePerOwinContext与创建InRequestScope相同吗?
是的,如果您愿意,可以以不同的方式将UserManager注入控制器,没有什么需要使用CreatePerOwinContext / IdentityFactoryMiddleware。
上述就是C#学习教程:使用带有IdentityFactory中间件的IOC容器分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1013103.html