Skip to main content

Explain the three DI lifetimes (Transient, Scoped, Singleton).

Entry .NET Core
Quick Answer Transient: new instance created every time the service is requested รขโ‚ฌโ€ for lightweight, stateless services. Scoped: one instance per HTTP request รขโ‚ฌโ€ for DbContext and request-specific services. Singleton: one instance for the app lifetime รขโ‚ฌโ€ for caches, config wrappers, and truly shared services. Injecting a Scoped service into a Singleton causes a captive dependency bug.

Answer

ASP.NET Core's DI container supports three main lifetimes:

  • Transient โ€“ A new instance is created every time it is requested. Best for lightweight, stateless services.
  • Scoped โ€“ One instance per HTTP request. Ideal for services that work with per-request data such as unit-of-work or context services.
  • Singleton โ€“ A single instance is created and shared for the lifetime of the application. Must be thread-safe and used carefully around shared state.

Choosing the wrong lifetime can lead to concurrency bugs, memory leaks, or stale data.

S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice