Exception-部分服务无法构建 asp.net core 3.1
Exception-Some services are not able to be constructed asp.net core 3.1
尝试通过向 IdentityUser 添加字段并覆盖 SignInManager 中的方法来自定义 asp.net 核心 3.1 身份并在项目启动时获取此异常
System.AggregateException
HResult=0x80131500
Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory`1[id1.AxUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory`1[id1.AxUser]': Unable to resolve service for type 'id1.AxUserStore' while attempting to activate 'id1.AxUserManager'.)
..
..
我认为调用的顺序可能搞砸了,但尝试移动它们并没有帮助,这是它的样子
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AxDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<AxUser>(options => { options.SignIn.RequireConfirmedAccount = true; options.User.RequireUniqueEmail = false; })
.AddEntityFrameworkStores<AxDbContext>()
.AddUserStore<AxUserStore>()
.AddUserManager<AxUserManager>()
.AddDefaultTokenProviders()
.AddSignInManager<AxSignInManager>()
;
//.AddClaimsPrincipalFactory<AxUserClaimsPrincipalFactory>()
其他代码是
public class AxUser : IdentityUser { // complete class
[PersonalData]public int SiteID { get; set; }
}
public class AxDbContext : IdentityDbContext<AxUser> { // complete class
public AxDbContext(DbContextOptions<AxDbContext> options): base(options){}
}
public class AxUserStore : UserStore<AxUser>{
public AxUserStore(AxDbContext ctx, IdentityErrorDescriber errorDescriber) :base(ctx, errorDescriber) {}
.... //other methods
}
public class AxUserManager : UserManager<AxUser> { // complete class !
public AxUserManager(AxUserStore store, IOptions<IdentityOptions> optionsAccessor,
IPasswordHasher<AxUser> passwordHasher, IEnumerable<IUserValidator<AxUser>> userValidators,
IEnumerable<IPasswordValidator<AxUser>> passwordValidators, ILookupNormalizer keyNormalizer,
IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<AxUser>> logger)
: base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger){ }
}
// complete class
public class AxUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<AxUser, IdentityRole> {
public AxUserClaimsPrincipalFactory(AxUserManager userManager,
RoleManager<IdentityRole> roleManager,
IOptions<IdentityOptions> optionsAccessor)
: base(userManager, roleManager, optionsAccessor) { }
}
public class AxSignInManager : SignInManager<AxUser>{
public AxSignInManager(UserManager<AxUser> userManager, IHttpContextAccessor contextAccessor, UserClaimsPrincipalFactory<AxUser> claimsFactory, IOptions<IdentityOptions> optionsAccessor,
ILogger<AxSignInManager> logger, IAuthenticationSchemeProvider schemes, IUserConfirmation<AxUser> confirmation)
:base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes, confirmation) { }
}
我只需要覆盖 UserStore 和 SignInManager 中的几个方法,类 的其余部分是在我试图让它不会在启动时崩溃时创建的!
感谢任何帮助
不确定您的其他代码是什么,只需使用您给定的代码进行如下更改即可。然后应用程序运行时不会抛出异常:
services.AddDbContext<AxDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<AxUser,IdentityRole>(options => { options.SignIn.RequireConfirmedAccount = true; options.User.RequireUniqueEmail = false; })
.AddEntityFrameworkStores<AxDbContext>()
.AddDefaultTokenProviders();
services.AddScoped<AxUserStore>();
services.AddScoped<AxUserManager>();
services.AddScoped<AxSignInManager>();
services.AddScoped<AxUserClaimsPrincipalFactory>();
services.AddScoped<UserClaimsPrincipalFactory<AxUser>>();
详细解释你可以查看下面的答案:
DI in general is intended for interface-driven development; .AddUserManager() specifies an implementation UserManager<>, not the service interface. That means that it's still expecting you to get UserManager and only use it that way; it'll give you an ApplicationUserManager.
尝试通过向 IdentityUser 添加字段并覆盖 SignInManager 中的方法来自定义 asp.net 核心 3.1 身份并在项目启动时获取此异常
System.AggregateException
HResult=0x80131500
Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory`1[id1.AxUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory`1[id1.AxUser]': Unable to resolve service for type 'id1.AxUserStore' while attempting to activate 'id1.AxUserManager'.)
..
..
我认为调用的顺序可能搞砸了,但尝试移动它们并没有帮助,这是它的样子
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AxDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<AxUser>(options => { options.SignIn.RequireConfirmedAccount = true; options.User.RequireUniqueEmail = false; })
.AddEntityFrameworkStores<AxDbContext>()
.AddUserStore<AxUserStore>()
.AddUserManager<AxUserManager>()
.AddDefaultTokenProviders()
.AddSignInManager<AxSignInManager>()
;
//.AddClaimsPrincipalFactory<AxUserClaimsPrincipalFactory>()
其他代码是
public class AxUser : IdentityUser { // complete class
[PersonalData]public int SiteID { get; set; }
}
public class AxDbContext : IdentityDbContext<AxUser> { // complete class
public AxDbContext(DbContextOptions<AxDbContext> options): base(options){}
}
public class AxUserStore : UserStore<AxUser>{
public AxUserStore(AxDbContext ctx, IdentityErrorDescriber errorDescriber) :base(ctx, errorDescriber) {}
.... //other methods
}
public class AxUserManager : UserManager<AxUser> { // complete class !
public AxUserManager(AxUserStore store, IOptions<IdentityOptions> optionsAccessor,
IPasswordHasher<AxUser> passwordHasher, IEnumerable<IUserValidator<AxUser>> userValidators,
IEnumerable<IPasswordValidator<AxUser>> passwordValidators, ILookupNormalizer keyNormalizer,
IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<AxUser>> logger)
: base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger){ }
}
// complete class
public class AxUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<AxUser, IdentityRole> {
public AxUserClaimsPrincipalFactory(AxUserManager userManager,
RoleManager<IdentityRole> roleManager,
IOptions<IdentityOptions> optionsAccessor)
: base(userManager, roleManager, optionsAccessor) { }
}
public class AxSignInManager : SignInManager<AxUser>{
public AxSignInManager(UserManager<AxUser> userManager, IHttpContextAccessor contextAccessor, UserClaimsPrincipalFactory<AxUser> claimsFactory, IOptions<IdentityOptions> optionsAccessor,
ILogger<AxSignInManager> logger, IAuthenticationSchemeProvider schemes, IUserConfirmation<AxUser> confirmation)
:base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes, confirmation) { }
}
我只需要覆盖 UserStore 和 SignInManager 中的几个方法,类 的其余部分是在我试图让它不会在启动时崩溃时创建的!
感谢任何帮助
不确定您的其他代码是什么,只需使用您给定的代码进行如下更改即可。然后应用程序运行时不会抛出异常:
services.AddDbContext<AxDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<AxUser,IdentityRole>(options => { options.SignIn.RequireConfirmedAccount = true; options.User.RequireUniqueEmail = false; })
.AddEntityFrameworkStores<AxDbContext>()
.AddDefaultTokenProviders();
services.AddScoped<AxUserStore>();
services.AddScoped<AxUserManager>();
services.AddScoped<AxSignInManager>();
services.AddScoped<AxUserClaimsPrincipalFactory>();
services.AddScoped<UserClaimsPrincipalFactory<AxUser>>();
详细解释你可以查看下面的答案:
DI in general is intended for interface-driven development; .AddUserManager() specifies an implementation UserManager<>, not the service interface. That means that it's still expecting you to get UserManager and only use it that way; it'll give you an ApplicationUserManager.