Tutorials ASP.NET Core MVC Mastery
Service Lifetimes
On this page
Mastering Dependency Injection Lifetimes (Transient, Scoped, Singleton)
Understanding service lifetimes is the difference between a bug-free app and one that has massive data corruption in production.
1. Transient (The Disposable)
A new instance is created **Every Time** it is requested. Best for lightweight, stateless services like a logging helper or an email formatter.
2. Scoped (The Request Gateway)
A new instance is created once per **HTTP Request**. All components in that same request share the same instance. This is Mandatory for DbContexts and UserContexts.
3. Singleton (The Global Engine)
One instance is created once, and it **Never Dies** until the server restarts. Best for caching engines or global configuration handlers.
Senior Audit: Captive Dependencies
Never inject a Scoped service into a Singleton. This is a Captive Dependency—the singleton will "capture" the scoped instance and keep it alive forever, leading to catastrophic database connection leaks and data corruption. This is an immediate fail in any architect-level interview.