将IOC容器与IdentityFactory中间件一起使用

编程入门 行业动态 更新时间:2024-10-27 18:24:11
本文介绍了将IOC容器与IdentityFactory中间件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图了解此处解释的UserManagerFactory中间件针对Usermanager的每个请求生存期管理.

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中间件一起使用

本文发布于:2023-11-08 09:36:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569017.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   中间件   IOC   IdentityFactory

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!