service lifetime
How long the container keeps and reuses an instance of a service: transient, scoped or singleton.
In more detail
Choose it by asking where the state belongs: nowhere (transient), to one request (scoped), or to the whole process (singleton). A longer-lived service must never hold a shorter-lived one.
Related terms
- transient lifetime — A service lifetime in which the container creates a new instance every time the service is requested.
- scoped lifetime — A service lifetime in which one instance is shared within a scope, which in ASP.NET Core means one HTTP request, then disposed.
- singleton — A service lifetime, or design, in which one instance is created and shared for the whole life of the process.
- scope validation — A container check that fails when a singleton depends on a scoped service, a bug called a captive dependency.
Taught in
- The Dependency Injection Container: Scopes and Lifetimes — Modern .NET Core, C# & Enterprise Microservices
- The Options Pattern: Strongly Typed Configuration — Modern .NET Core, C# & Enterprise Microservices