Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'UserSeeder'. means a class asked for UserManager<IdentityUser> and dependency injection has none for that user type. Register Identity for the same user class the constructor names, with AddIdentityCore<IdentityUser>().AddEntityFrameworkStores<AppDb>().

The type inside the square brackets is the one to match. In the lab two different mistakes printed the same line: Identity not registered at all, and Identity registered for a custom AppUser while the seeder still asked for UserManager<IdentityUser>.

The error

Started with dotnet run --no-build, whose launch profile sets ASPNETCORE_ENVIRONMENT to Development, the app stopped at builder.Build(); the exception came on the error stream:

Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: UserSeeder Lifetime: Scoped ImplementationType: UserSeeder': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'UserSeeder'.)
 ---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: UserSeeder Lifetime: Scoped ImplementationType: UserSeeder': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'UserSeeder'.
 ---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'UserSeeder'.
...

Why it happens

The first Program.cs registered the database context and the seeder, but no Identity call at all, so nothing in the container could build a UserManager:

...
builder.Services.AddDbContext<AppDb>(o => o.UseSqlite("Data Source=lab.db"));
builder.Services.AddScoped<UserSeeder>();
...

The second version did call AddIdentityCore<AppUser>(), with AppDb : IdentityDbContext<AppUser>, and kept class UserSeeder(UserManager<IdentityUser> users). It printed the same three lines word for word. UserManager<AppUser> and UserManager<IdentityUser> are different types to the container, even though AppUser derives from IdentityUser.

The stack trace pointed at the line with builder.Build(), not at the seeder call, and the message starts with "Error while validating the service descriptor" for UserSeeder. The check ran while the container was being built, so the app stopped before EnsureCreated(), before the seeder and before it listened on any port.

The same broken code started from the built DLL without a launch profile printed Hosting environment nowhere, because it never got that far, but it failed later: the container was not validated at Build(), and the stack pointed at the GetRequiredService<UserSeeder>() line inside the scope, with only the innermost line of the message:

Unhandled exception. System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'UserSeeder'.
...

The fix

...
builder.Services.AddDbContext<AppDb>(o => o.UseSqlite("Data Source=lab.db"));
builder.Services.AddIdentityCore<IdentityUser>()
    .AddEntityFrameworkStores<AppDb>();
builder.Services.AddScoped<UserSeeder>();
...
class AppDb(DbContextOptions<AppDb> options) : IdentityDbContext<IdentityUser>(options);
...

With those lines the app started and the seeder printed Seed: Succeeded. For the second cause the registration was already right; the constructor was wrong. Changing it to class UserSeeder(UserManager<AppUser> users) and creating an AppUser also gave Seed: Succeeded. Pick one user class and use it in the registration, the IdentityDbContext<T> and every UserManager<T> you inject.

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, a SQLite file in the project folder, and a scoped UserSeeder resolved from a scope after builder.Build() that creates one fictional user. UserSeeder, AppDb, AppUser and lab.db are the test's own names. The two broken versions were started with dotnet run --no-build (Development, from the launch profile); the fixed versions and the second look at the first broken one with dotnet bin\Debug\net10.0\IdnLab.dll --urls http://127.0.0.1:15620, which printed Hosting environment: Production when it got as far as listening.

Frequently asked

Why can't ASP.NET Core resolve UserManager<IdentityUser>?
Nothing registered Identity for that user type. Add builder.Services.AddIdentityCore<IdentityUser>().AddEntityFrameworkStores<AppDb>() before builder.Build(), with a DbContext that derives from IdentityDbContext<IdentityUser>.
I registered Identity with a custom user class, why does UserManager still fail?
The container looks for the exact generic type. If you registered AddIdentityCore<AppUser>(), inject UserManager<AppUser>, not UserManager<IdentityUser>. In the lab both mistakes printed the same message.
Where do I find which user type is missing in the error?
Inside the square brackets after UserManager`1. The lab printed UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser], so the constructor asked for UserManager<IdentityUser>.

The general form of this message, for any missing service, is in Unable to resolve service for type while attempting to activate. More decoded errors in the Fixes category.