ASP.NET Core › Dependency Injection

What is the difference between Singleton, Scoped, and Transient service lifetimes, and how does Scoped specifically map to an HTTP request?

Intermediate

Answer

Singleton creates one instance for the entire application lifetime, shared across all requests. Scoped creates one instance per client HTTP request (a new DI scope is created at the start of each request and disposed at its end), 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 HTTP 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.

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?