What is the Captive Dependency problem?
Updated Apr 28, 2026
Short answer
A Captive Dependency occurs when a service with a longer lifetime holds a reference to a service with a shorter lifetime.
Deep explanation
The most common example is injecting a Transient or Scoped dependency into a Singleton. Because the Singleton is only created once, the injected dependency is also trapped and behaves like a Singleton, defeating its intended lifetime and causing concurrency bugs or memory leaks.
Real-world example
Injecting an HTTP-request scoped database context into a Singleton background worker, causing the DbContext to be shared across all users and eventually crash.
Common mistakes
- Assuming the DI container will magically refresh the transient dependency inside the singleton on every method call.
Follow-up questions
- How do modern DI containers prevent captive dependencies?