No service for type 'Microsoft.AspNetCore.Identity.IEmailSender`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered.
was thrown by app.MapIdentityApi<IdentityUser>() when Identity had been
registered with AddIdentityCore alone. Add .AddApiEndpoints() to the
Identity registration and the app starts.
The exception came at startup, from the line that maps the endpoints, not from a request:
nothing was listening yet, so the first curl.exe to /register got no
connection at all.
The error
Server console, at startup, on the error stream:
Unhandled exception. System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.IEmailSender`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered.
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.AspNetCore.Routing.IdentityApiEndpointRouteBuilderExtensions.MapIdentityApi[TUser](IEndpointRouteBuilder endpoints)
...
Why it happens
The registration in the lab set up the bearer token scheme, authorization and the Identity core services, and mapped the Identity API endpoints:
...
builder.Services.AddAuthentication().AddBearerToken(IdentityConstants.BearerScheme);
builder.Services.AddAuthorization();
builder.Services.AddIdentityCore<IdentityUser>()
.AddEntityFrameworkStores<AppDb>();
...
app.MapIdentityApi<IdentityUser>();
...
The stack trace shows MapIdentityApi calling GetRequiredService for an
IEmailSender<IdentityUser> while it maps the endpoints. Nothing in this
registration had added one, so the call threw before app.Run(). The broken run's console
ended with the stack trace and never printed the Now listening on line, and the
stack trace named line 18 of Program.cs, the MapIdentityApi call.
The fix
...
builder.Services.AddIdentityCore<IdentityUser>()
.AddEntityFrameworkStores<AppDb>()
.AddApiEndpoints();
...
With that one line the console printed Now listening on: http://127.0.0.1:15620,
and a register request answered
HTTP/1.1 200 OK:
curl.exe -s -i -X POST -H "Content-Type: application/json" -d "@register.json" http://127.0.0.1:15620/register
HTTP/1.1 200 OK
Content-Length: 0
...
The body file held a fictional address and the test password,
{"email":"tomas.silva@example.test","password":"Spring-2026x"}. The lab checked that the app
starts and that /register answers; it had no mail server and looked for no mail.
When the app must send real mail, register your own class that implements
IEmailSender<IdentityUser>; that part was not tested here.
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 created with
EnsureCreated(), and MapIdentityApi<IdentityUser>() as the Identity
feature that asks for the sender. AppDb, lab.db, register.json and
the user in it are the test's. Both versions were built with dotnet build and started with
dotnet bin\Debug\net10.0\IdnLab.dll --urls http://127.0.0.1:15620 (the fixed one printed
Hosting environment: Production), and the request sent with curl.exe from the
project folder; in the broken run curl exited with code 7, as the app had already stopped.
Frequently asked
- Why does MapIdentityApi say no service for type IEmailSender has been registered?
- Identity was registered with AddIdentityCore and nothing registered IEmailSender<IdentityUser>. Add .AddApiEndpoints() to the Identity builder; in the lab that let the app start. A sender of your own was not tested here.
- Does AddApiEndpoints send confirmation emails?
- The lab only showed that the app starts and /register answers 200 OK with it; no mail server was set up. To send real mail, register your own IEmailSender<IdentityUser> implementation.
- When does the IEmailSender error appear, at startup or on a request?
- At startup. The stack trace runs through MapIdentityApi, before app.Run(), so the app never listened and a request to /register could not connect.
A registration that is missing UserManager itself prints a different message, covered in
Unable to
resolve service for type UserManager. More decoded errors in the
Fixes category.