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

Entry .NET Core

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.

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