No service for type 'Microsoft.AspNetCore.Identity.RoleManager error
Solution 1:
You can inject any registered service explicitly into the Configure()
method.
public void Configure(RoleManager<IdentityRole> roleManager)
I'm not sure what is happening when you try to inject IServiceProvider
, but it doesn't look correct.
Also, never use .Wait()
, use .GetAwaiter().GetResult()
instead.
Solution 2:
I have figured it out.
I created a new ApplicationUser class, which inherited from it IdentityUser. Afterwards I ran the Identity scaffolder, stating to use my ApplicationUser as the new class.
While doing that .NET CORE created an additional class:
public class IdentityHostingStartup : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) => {
services.AddDbContext<ApplicationDBContext>(options =>
options.UseSqlServer(
context.Configuration.GetConnectionString("ApplicationDBContextConnection")));
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDBContext>();
});
}
}
The configuration in this class overrides every option and service ( that has been declared ) in the startup class. And it will crash if you have identical options/services declared in both classes.. That's why It wasn't working. After adding .AddRoles<IdentityRole>()
to the IdentityHostingStartUp everything is working!
I'm still looking for a way to rip out the IdentityHostingStartUp, just ripping out those declared there will let the application crash.