Dependency injection InvalidOperationException:检测到类型为';Microsoft.AspNetCore.Identity.UserManager
这是在我自定义asp.net核心标识服务以支持基于的多租户之后发生的。我简化了它以满足我的需要 这是我的基本设置 1) 自定义应用程序用户Dependency injection InvalidOperationException:检测到类型为';Microsoft.AspNetCore.Identity.UserManager,dependency-injection,asp.net-core-mvc,asp.net-identity-3,Dependency Injection,Asp.net Core Mvc,Asp.net Identity 3,这是在我自定义asp.net核心标识服务以支持基于的多租户之后发生的。我简化了它以满足我的需要 这是我的基本设置 1) 自定义应用程序用户 public class ApplicationUser : IdentityUser<int>, IEntityBase{} 10) 我有一个basecontroller,希望通过构造函数注入 public BaseController(ApplicationDbContext dbContext, UserManager<A
public class ApplicationUser : IdentityUser<int>, IEntityBase{}
10) 我有一个basecontroller,希望通过构造函数注入
public BaseController(ApplicationDbContext dbContext,
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IMessageServices messageServices, ILoggerFactory loggerFactory,
AppTenant currentTenant, IMapper mapper)
{
_dbContext = dbContext;
_signInManager = signInManager;
_userManager = userManager;
_messageServices = messageServices;
_logger = loggerFactory.CreateLogger<BaseController>();
_currentTenant = currentTenant;
_mapper = mapper;
}
堆栈跟踪显示了所有框架代码,我发现很难找出循环引用
有人能给我指出正确的方向吗?找到了问题。在课堂上
public class ApplicationUserStore : UserStoreMultiTenant<ApplicationUser, ApplicationRole, int>{}
公共类ApplicationUserStore:UserStoreMultiTenant{}
我在构造函数中请求UserManager,这导致了问题。如果框架显示我的代码中哪一行导致它失败,那就太好了。这意味着
UserManager
依赖于一个类型,该类型要么直接依赖于UserManager
,要么依赖于a=>B,B=>C,C=>a
“如果框架显示我的代码中哪一行导致失败,那就太好了。“如果您直接从代码解析服务,DI容器几乎不可能帮助您解决这一问题;服务定位器反模式使DI容器无法完成其工作,这是服务定位器被称为反模式的众多原因之一。
public class UserStoreMultiTenant<TUser, TRole, TKey> : UserStore<TUser, TRole, ApplicationDbContext, int>{}
public class ApplicationRoleStore : RoleStoreMultiTenant<ApplicationRole>{}
public class ApplicationUserStore : UserStoreMultiTenant<ApplicationUser, ApplicationRole, int>{}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>{}
services.AddScoped<IRoleStore<ApplicationRole>, ApplicationRoleStore>();
services.AddScoped<IUserStore<ApplicationUser>, ApplicationUserStore>();
services.AddIdentity<ApplicationUser, ApplicationRole>(o =>
{
o.User.RequireUniqueEmail = true;
//options code
}).AddUserStore<ApplicationUserStore>()
.AddEntityFrameworkStores<ApplicationDbContext, int>();
//other code
app.UseIdentity();
//other code
public BaseController(ApplicationDbContext dbContext,
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IMessageServices messageServices, ILoggerFactory loggerFactory,
AppTenant currentTenant, IMapper mapper)
{
_dbContext = dbContext;
_signInManager = signInManager;
_userManager = userManager;
_messageServices = messageServices;
_logger = loggerFactory.CreateLogger<BaseController>();
_currentTenant = currentTenant;
_mapper = mapper;
}
InvalidOperationException: A circular dependency was detected for the service of type 'Microsoft.AspNetCore.Identity.UserManager`1[Registrar.Data.MultitenantIdentity.Models.ApplicationUser]'.
public class ApplicationUserStore : UserStoreMultiTenant<ApplicationUser, ApplicationRole, int>{}