ASP.NET Core hỗ trợ ba lifetime, lựa chọn theo đặc tính stateful của service.
- Transient: instance mới mỗi lần resolve — stateless utilities, helper
- Scoped: cùng instance trong một HTTP request —
DbContext,UnitOfWork - Singleton: một instance duy nhất suốt đời app — logging, configuration, in-memory cache
Không bao giờ inject Scoped vào Singleton — gây "captive dependency" bug nghiêm trọng (xem #2775).
ASP.NET Core offers three lifetimes; pick based on the service's stateful characteristics.
- Transient: a new instance per resolve — stateless utilities, helpers
- Scoped: the same instance per HTTP request —
DbContext,UnitOfWork - Singleton: one instance for the application lifetime — logging, configuration, in-memory cache
Never inject a Scoped service into a Singleton — it creates a "captive dependency" bug (see #2775).