Dependency injection · interview question
A teammate describes their service registrations. Highlight the statements that describe a lifetime bug.
Highlight textHard
Select the findings that apply.
Answer and explanation
Correct answer
- ReportCache takes AppDbContext in its constructor, and AppDbContext is registered as scoped.
- A background timer stores an OrderService instance in a static field and reuses it for days.
Why
The rule: a service must not depend on anything with a shorter lifetime. ASP.NET Core's scope validation (on by default in Development) throws for scoped-in-singleton registrations.
Test yourself on Dependency injection
A free test with instant, explained results. No account needed.