Exception-Some services are not able to be constructed asp.net core 3.1

-1

Trying to customize asp.net core 3.1 identity with adding a field to IdentityUser and override a method in SignInManager & Getting this exception in project startup

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'.)
..
..

I think the order in which the calls are made is messed up maybe but trying to move them around didn't help, here's what it looks like

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>()

other code is

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) { }

    }
         

I only need to override couple of methods in UserStore and SignInManager, rest of classes were created as i'm trying to get it to NOT crash on startup !

any help is appreciated

asp.net
asp.net-core
dependency-injection
asp.net-identity
asked on Stack Overflow May 9, 2021 by Kumar • edited May 10, 2021 by Kumar

1 Answer

0

Not sure what is your other code, just change like below by using your given code. Then it will not throw exception when the application runs:

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>>();

Detailed explanation you could check the answer below:

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.

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

answered on Stack Overflow May 10, 2021 by Rena

User contributions licensed under CC BY-SA 3.0