No service for type 'Microsoft.AspNetCore.Identity.RoleManager`1[Microsoft.AspNetCore.Identity.IdentityRole]' has been registered.
was thrown when a seed asked for RoleManager<IdentityRole> and Identity had been
registered with AddIdentityCore and no roles. Add
.AddRoles<IdentityRole>() to the Identity builder, before
.AddEntityFrameworkStores<AppDb>().
The order is part of the fix: with AddRoles after the stores, the lab got a
different message about IRoleStore, quoted below.
The error
Server console, at startup, on the error stream:
Unhandled exception. System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.RoleManager`1[Microsoft.AspNetCore.Identity.IdentityRole]' has been registered.
...
Why it happens
The registration had users only, and the seed asked for the role manager:
...
builder.Services.AddIdentityCore<IdentityUser>()
.AddEntityFrameworkStores<AppDb>();
...
var roles = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
...
AddIdentityCore<IdentityUser>() takes no role type, so nothing in this registration
put a RoleManager<IdentityRole> into the container, and
GetRequiredService threw on that line (the stack trace named it). The seed asked the
container directly with GetRequiredService; a class that takes
RoleManager in its constructor was not tried here, and may word the message
differently.
The fix
...
builder.Services.AddIdentityCore<IdentityUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<AppDb>();
...
With that line the seed printed Create role: Succeeded. A third run of this same lab, with only the two
builder calls swapped (.AddEntityFrameworkStores<AppDb>() first, then
.AddRoles<IdentityRole>()), failed on the role manager's store:
Unhandled exception. System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IRoleStore`1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.RoleManager`1[Microsoft.AspNetCore.Identity.IdentityRole]'.
...
So if you already have AddRoles and still see an error about roles, check its place in
the chain first; in this lab the position alone decided which of the two messages appeared.
How it was reproduced
A dotnet new web -f net10.0 project on .NET SDK 11.0.100 with
Microsoft.AspNetCore.Identity.EntityFrameworkCore 10.0.10 and
Microsoft.EntityFrameworkCore.Sqlite 10.0.10, and a seed in a scope after
builder.Build() that calls EnsureCreated(), resolves the role manager and
creates a role named Clinician. IdnLab, port 15620, AppDb, lab.db
and the role name are the test's. All three runs were built with dotnet build and started with
dotnet bin\Debug\net10.0\IdnLab.dll --urls http://127.0.0.1:15620: the broken one in a
new copy of the base project (the evidence records no lab.db before it), then the fix, then the
swapped order, each later run after moving the previous file aside. A copy of the same
other-order message from the IUserRoleStore post's lab is also kept in the evidence.
Frequently asked
- Why is RoleManager<IdentityRole> not registered?
- Identity was registered with AddIdentityCore<IdentityUser>() and no roles. Add .AddRoles<IdentityRole>() before .AddEntityFrameworkStores<AppDb>(); in the lab the role was then created.
- Does the order of AddRoles and AddEntityFrameworkStores matter?
- In the lab, yes. With AddRoles after AddEntityFrameworkStores, resolving RoleManager failed with Unable to resolve service for type IRoleStore<IdentityRole>.
- What is the difference between this error and Store does not implement IUserRoleStore?
- This one came from asking the container for RoleManager. The IUserRoleStore message is covered in its own post, linked at the end of this one.
A related role message, from AddToRoleAsync, is covered in
'Store does not
implement IUserRoleStore<TUser>'. More decoded errors in the
Fixes category.