C# & CLR Internals (50 Qs)•Part 3: CLR Internals, Native AOT & Production Diagnostics (25 - 35)
Dependency Injection: The Captive Dependency Problem.
senior LevelMost AskedDependency Injection
In Simple Words: .NET provides three DI lifetimes:
- Transient: Created every time requested.
- Scoped: Created once per client request (HTTP request).
- Singleton: Created once for the application lifetime.
- The Captive Dependency Bug: If a Singleton service injects a Scoped service (like an EF Core
DbContext), the scoped service is kept alive forever by the singleton! This leads to memory leaks, concurrency crashes, and reading stale database state.