ASP.NET Core Essentials
Middleware pipeline, Minimal APIs vs Controllers, DI lifetimes.
Middleware pipeline — an ordered chain of delegates; each can act before/after calling next().
Order matters: e.g. UseExceptionHandler before UseRouting, UseAuthentication before
UseAuthorization, UseAuthorization before endpoint execution.
Minimal APIs vs Controllers — both are valid in .NET 8+; Minimal APIs reduce ceremony for small
services/endpoints, Controllers still make sense for larger surface areas with shared conventions
(model binding, filters, [ApiController] validation). Be ready to say you'd pick based on team
size and endpoint count, not because one is "outdated."
DI service lifetimes — the single most commonly-probed ASP.NET Core question:
- Transient: new instance every time it's requested.
- Scoped: one instance per HTTP request (or per scope you create manually).
- Singleton: one instance for the app's lifetime.
The classic trap: injecting a Scoped (e.g. DbContext) into a Singleton — the singleton captures
the first request's scoped instance forever, causing subtle bugs or an exception if you have
ValidateScopes enabled (on by default in Development). Fix: inject IServiceScopeFactory into the
singleton and create a scope per use.