Guest MemberLanguage   English
.NET & C# · ASP.NET Core

How would you solve an interview scenario involving dependency injection lifetimes?

AdvancedUpdated 2026-08-08

For a practical scenario involving dependency injection lifetimes, I would first clarify the business goal, scale, constraints, and the failure or quality attribute being tested. ASP.NET Core has transient, scoped, and singleton service lifetimes. Transient creates a new instance per resolution, scoped usually creates one per request, and singleton creates one for the application lifetime. The scenario centers on a BackgroundService is singleton but needs a DbContext. Inject IServiceScopeFactory, create a scope for each unit of work, and resolve the scoped DbContext from that scope. I would then compare the main alternatives and tradeoffs, identify likely failure modes, and explain how I would validate the solution through testing, observability, security controls, and recovery or rollback planning.

#aspnet-core#dependency injection lifetimes