.NET › Dependency Injection

What is the difference between Singleton, Scoped, and Transient service lifetimes in ASP.NET Core's DI container?

Intermediate

Answer

Singleton creates one instance for the entire application lifetime, shared across all requests and threads. Scoped creates one instance per client request (or per explicitly created scope), shared within that single request but distinct across different requests. Transient creates a brand-new instance every single time the service is requested, even multiple times within the same request.
builder.Services.AddSingleton<ICacheService, MemoryCacheService>();  // one instance, app lifetime
builder.Services.AddScoped<AppDbContext>();                          // one instance per request
builder.Services.AddTransient<IEmailFormatter, EmailFormatter>();    // new instance every injection
Real-world example A DbContext is registered as Scoped so all repositories within a single HTTP request share the same context (and its change tracking), while a lightweight, stateless formatter is registered as Transient since creating a new instance each time is cheap and avoids any shared-state concerns.

Common follow-ups: What happens if a Singleton service depends on a Scoped service (captive dependency problem)?;How do you choose the right lifetime for a new service?