Когда я запускаю свое приложение в режиме отладки, мой метод семени не работает из-за «отсутствующей» службы.Сообщение об ошибке:
Нет службы для типа 'Microsoft.AspNetCore.Identity.RoleManager`1 [Microsoft.AspNetCore.Identity.IdentityRole]'.
Может ли кто-нибудь помочь мне правильно зарегистрировать эту услугу в классе StartUp.cs
?Спасибо!
RolesConfig.cs
public static class RolesConfig
{
public static async Task InitialiseAsync(ApplicationDbContext context, IServiceProvider serviceProvider)
{
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
string[] roleNames = {"Admin", "Report", "Search"};
foreach (var roleName in roleNames)
{
var roleExist = await roleManager.RoleExistsAsync(roleName);
if (!roleExist)
await roleManager.CreateAsync(new IdentityRole(roleName));
}
}
}
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddHttpClient();
services.AddHttpClient<IExceptionServiceClient, ExceptionServiceClient>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}