← All topics
🧰 Core Tech Refresher

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.

Flashcards (4)

What breaks if you inject a Scoped service into a Singleton?
tap to reveal answer
The Singleton captures the Scoped instance from whichever request first resolved it and reuses it forever — e.g. a DbContext shared across all requests. ASP.NET Core throws in Development by default (scope validation).
How do you correctly use a Scoped/DbContext-dependent service from within a Singleton?
tap to reveal answer
Inject IServiceScopeFactory, call CreateScope() when you need the work done, resolve the scoped service from that scope, and dispose the scope afterwards.
Name two things that must happen in a specific middleware order and why.
tap to reveal answer
UseAuthentication before UseAuthorization (must know WHO before deciding WHAT they can do); UseExceptionHandler/UseHsts early so they wrap everything downstream.
When would you choose Controllers over Minimal APIs for a new ASP.NET Core 8 service?
tap to reveal answer
Larger endpoint surface needing shared conventions — model binding, [ApiController] automatic 400s, action filters, versioning conventions — where the structure pays for itself. Minimal APIs shine for small, focused services.