【问题标题】:Exception-Some services are not able to be constructed asp.net core 3.1异常-asp.net core 3.1 部分服务无法构建
【发布时间】:2021-07-31 08:22:57
【问题描述】:

尝试通过向 IdentityUser 添加字段并覆盖 SignInManager 中的方法并在项目启动中获取此异常来自定义 asp.net core 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 中的几个方法,其余的类都是在我试图让它在启动时不崩溃时创建的!

感谢任何帮助

【问题讨论】:

    标签: asp.net asp.net-core dependency-injection asp.net-identity


    【解决方案1】:

    不确定您的其他代码是什么,只需使用您给定的代码进行如下更改即可。那么应用运行时就不会抛出异常:

    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 通常用于接口驱动的开发; .AddUserManager() 指定实现 UserManager,而不是服务接口。这意味着它仍然希望您获得 UserManager 并仅以这种方式使用它;它会给你一个 ApplicationUserManager。

    https://stackoverflow.com/a/30457561/11398810

    【讨论】:

    • 试过这个,现在得到这个错误 - 验证服务描述符时出错“ServiceType: id1.AxUserClaimsPrincipalFactory Lifetime: Scoped ImplementationType: id1.AxUserClaimsPrincipalFactory”: Unable to resolve service for type 'Microsoft.AspNetCore.Identity .RoleManager`1[Microsoft.AspNetCore.Identity.IdentityRole]' 尝试激活 'id1.AxUserClaimsPrincipalFactory'。)'
    • 它在我的项目中运行良好。请务必将services.AddDefaultIdentity&lt;AxUser&gt; 更改为services.AddIdentity&lt;AxUser,IdentityRole&gt;
    • 啊,成功了,谢谢,我之前错过了!
    猜你喜欢
    • 2022-01-11
    • 2022-01-21
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 2021-12-09
    • 2015-04-03
    相关资源
    最近更新 更多