我正在使用 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'.)'
配置服务方法:
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 = "[email protected]"
};
var defaultAdminTask = await userManager.CreateAsync(defaultAdmin, "MYP@ssword2021");
if (defaultAdminTask.Succeeded)
{
var adminToRoleTask = await userManager.AddToRoleAsync(defaultAdmin, Roles.Admin.ToString());
}
}
}
请有关此问题的任何帮助??
我建议将播种移至
Configure()
,因为在 ConfigureServices()
中,ServiceProvider
尚未构建,因为您刚刚配置它。
你可以调用
services.BuildServiceProvider()
,但这是有问题的,而且没有必要这样做,因为你可以在Configure()
中很好地实现播种。
主要是在一个新的范围内进行整个播种,然后处理它。
这是一个可靠的方法,例如:
public async Task Configure(IApplicationBuilder app)
{
...
await CreateDefaultRoles(app);
...
}
public async Task CreateDefaultRoles(IApplicationBuilder appBuilder)
{
using (var scope = appBuilder.ApplicationServices.CreateScope())
{
var serviceProvider = scope.ServiceProvider;
var userManager = serviceProvider.GetRequiredService<UserManager<AppUser>>();
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
// Do your seeding stuff
}
}
我只能假设
Configure()
作为 async
方法工作;从未尝试过以这种方式使用它。我只是使用同步方法为数据库播种。
如果此解决方案不适合您,请告诉我。
遇到类似的错误,我的问题是我忘记设置
ILogger
的类型。我原来的代码是:
public class MyClass
{
private readonly ILogger _logger;
通过设置
ILogger<MyClass>
修复(在私有字段和构造函数中修复):
public class MyClass
{
private readonly ILogger<MyClass> _logger;