访问 Microsoft.Extensions.Hosting 服务时出错。添加 ASP.NET 身份后
An error occurred while accessing the Microsoft.Extensions.Hosting services. After Adding ASP.NET identity
我在不同的项目中有 DataContext 和 StartUp class,为了在 Data
项目中添加新的迁移,我使用了以下命令:
dotnet ef migrations add IdentityAdded -s ..\API\API.csproj
这是项目结构:
我刚在基于.Net 5的项目中添加了ASP.Net Core Identity,配置如下:
public class DataContext : IdentityDbContext<AppUser, AppRole, int,
IdentityUserClaim<int>, AppUserRole, IdentityUserLogin<int>,
IdentityRoleClaim<int>, IdentityUserToken<int>>
{
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
ChangeTracker.LazyLoadingEnabled = false;
}
... DbSets
... protected override void OnModelCreating(ModelBuilder modelBuilder)
{ ... }
}
IdentityServiceExtension.cs:
public static class IdentityServiceExtension
{
public static IServiceCollection AddIdentityServices(this IServiceCollection services, IConfiguration configuration)
{
services.AddIdentityCore<AppUser>(opt =>
{
opt.Password.RequireNonAlphanumeric = false;
})
.AddRoles<AppRole>()
.AddRoleManager<RoleManager<AppRole>>()
.AddSignInManager<SignInManager<AppUser>>()
.AddRoleValidator<RoleValidator<AppUser>>()
.AddEntityFrameworkStores<DataContext>();
}
}
我刚刚从 Identity 类 继承了一些 class,例如 AppUser
、AppRole
和 AppUserRole
,如下所示:
public class AppRole : IdentityRole<int>
{
public ICollection<AppUserRole> TheUserRolesList { get; set; }
}
在 运行 迁移之后,我收到以下错误:
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.RoleManager1[Core.Models.Entities.User.AppRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.RoleManager
1[Core.Models.Entities.User.AppRole]': Implementation type 'Microsoft.AspNetCore.Identity.RoleValidator1[Core.Models.Entities.User.AppUser]' can't be converted to service type 'Microsoft.AspNetCore.Identity.IRoleValidator
1[Core.Models.Entities.User.AppRole]')
这个实现有什么问题?
您没有正确注册,而不是:
.AddRoleValidator<RoleValidator<AppUser>>()
添加:
.AddRoleValidator<RoleValidator<AppRole>>()
您的错误指出它无法使用 Core.Models.Entities.User.AppUser 实例化 Microsoft.AspNetCore.Identity.RoleValidator,而是需要 Core.Models.Entities.User.AppRole.
我在不同的项目中有 DataContext 和 StartUp class,为了在 Data
项目中添加新的迁移,我使用了以下命令:
dotnet ef migrations add IdentityAdded -s ..\API\API.csproj
这是项目结构:
我刚在基于.Net 5的项目中添加了ASP.Net Core Identity,配置如下:
public class DataContext : IdentityDbContext<AppUser, AppRole, int,
IdentityUserClaim<int>, AppUserRole, IdentityUserLogin<int>,
IdentityRoleClaim<int>, IdentityUserToken<int>>
{
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
ChangeTracker.LazyLoadingEnabled = false;
}
... DbSets
... protected override void OnModelCreating(ModelBuilder modelBuilder)
{ ... }
}
IdentityServiceExtension.cs:
public static class IdentityServiceExtension
{
public static IServiceCollection AddIdentityServices(this IServiceCollection services, IConfiguration configuration)
{
services.AddIdentityCore<AppUser>(opt =>
{
opt.Password.RequireNonAlphanumeric = false;
})
.AddRoles<AppRole>()
.AddRoleManager<RoleManager<AppRole>>()
.AddSignInManager<SignInManager<AppUser>>()
.AddRoleValidator<RoleValidator<AppUser>>()
.AddEntityFrameworkStores<DataContext>();
}
}
我刚刚从 Identity 类 继承了一些 class,例如 AppUser
、AppRole
和 AppUserRole
,如下所示:
public class AppRole : IdentityRole<int>
{
public ICollection<AppUserRole> TheUserRolesList { get; set; }
}
在 运行 迁移之后,我收到以下错误:
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.RoleManager
1[Core.Models.Entities.User.AppRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.RoleManager
1[Core.Models.Entities.User.AppRole]': Implementation type 'Microsoft.AspNetCore.Identity.RoleValidator1[Core.Models.Entities.User.AppUser]' can't be converted to service type 'Microsoft.AspNetCore.Identity.IRoleValidator
1[Core.Models.Entities.User.AppRole]')
这个实现有什么问题?
您没有正确注册,而不是:
.AddRoleValidator<RoleValidator<AppUser>>()
添加:
.AddRoleValidator<RoleValidator<AppRole>>()
您的错误指出它无法使用 Core.Models.Entities.User.AppUser 实例化 Microsoft.AspNetCore.Identity.RoleValidator,而是需要 Core.Models.Entities.User.AppRole.