2026-06-09 / C# / .NET Core
ASP.NET Core Dependency Injection and Lifetime Selection
DI lifetimes are not registration syntax. They are design choices about resources, thread safety, and request boundaries.
ASP.NET Core.NET
ASP.NET Core Dependency Injection and Lifetime Selection
Dependency injection is not framework magic. It separates object creation from object usage. The risky part is choosing the wrong lifetime.
flowchart TB
A["Application startup"] --> B["Singleton"]
C["Each HTTP request"] --> D["Scoped"]
E["Each resolve"] --> F["Transient"]
B --> G["Must be thread-safe"]
D --> H["Business service / DbContext"]
F --> I["Lightweight stateless object"]
Engineering Scenario
DbContext is usually scoped because it belongs to one request or unit of work. Injecting it into a singleton leaks request-level state into global lifetime.
Best Practices
- Start with Scoped for business services.
- Singleton must be stateless or thread-safe.
- Use Transient for lightweight objects.
- Design lifetime and disposal together.