【发布时间】:2021-06-06 08:23:00
【问题描述】:
我正在使用 Identity 进行 ASP.net core 项目。
我想做什么:
我尝试让网络应用创建新的默认用户和角色
一切都很好,直到我在 ConfigureServices 中添加并调用了 Startup 类中的方法
之后我收到此错误/异常
System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.Razor.Compilation.IViewCompilerProvider Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompilerProvider': Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager' while attempting to activate 'Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompilerProvider'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager': Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager' while attempting to activate 'Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.CSharpCompiler Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.CSharpCompiler': Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager' while attempting to activate 'Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager'.)'
ConfigureServices 方法:
public async Task ConfigureServices(IServiceCollection services, IServiceProvider serviceProvider)
{
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddIdentity<AppUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
.AddClaimsPrincipalFactory<UserClaimsPrincipalFactory<AppUser, IdentityRole>>()
.AddEntityFrameworkStores<AppDbContext>().AddDefaultTokenProviders().AddDefaultUI();
services.AddControllersWithViews();
services.AddRazorPages();
//This is the method I try to call
await CreateDefaultRoles(serviceProvider);
}
我尝试调用的方法:
public async Task CreateDefaultRoles(IServiceProvider serviceProvider)
{
var userManager = serviceProvider.GetRequiredService<UserManager<AppUser>>();
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
bool isAdminRoleExist = await roleManager.RoleExistsAsync("Admin");
if (!isAdminRoleExist)
{
var roleResult = await roleManager.CreateAsync(new IdentityRole(Roles.Admin.ToString()));
}
var defaultAdminUser = await userManager.FindByNameAsync("Admin");
if (defaultAdminUser == null)
{
AppUser defaultAdmin = new AppUser()
{
UserName = "Admin",
Email = "my@email.com"
};
var defaultAdminTask = await userManager.CreateAsync(defaultAdmin, "MYP@ssword2021");
if (defaultAdminTask.Succeeded)
{
var adminToRoleTask = await userManager.AddToRoleAsync(defaultAdmin, Roles.Admin.ToString());
}
}
}
那么请大家帮忙解决这个问题??
【问题讨论】:
-
据我了解ConfigureServices,不是你调用其他方法创建默认数据的地方。按照惯例,它应该用于注册服务和设置配置选项。错误是否是由于这个我不知道。
-
您使用的是哪个 .net 核心版本?我刚刚发现了一个新功能,它支持将更多服务注入
ConfigureServices:) - 因为正如我对.net core 2.2所经历的那样,这是不可能的,只有一个IServiceCollection被注入其中。对于您的错误,很容易理解,在构建 DI 容器时,您的身份服务(如UserManager、RoleManager......)尚未可用。容器还没有构建,所以你得到的IServiceProvider只是框架提供的另一个隐藏实例。 -
将迁移代码移至稍后阶段,例如在
Startup.Configure中配置应用程序构建器时。 -
我使用.Net 5 这个项目基于Asp.net core 5
标签: c# asp.net-core asp.net-identity identity