C# › Dependency Injection & IoC Principles
What is the difference between Singleton, Scoped, and Transient service lifetimes in ASP.NET Core's DI container?
IntermediateAnswer
Singleton creates ONE instance for the entire application's lifetime, shared across all requests; Scoped creates ONE instance PER REQUEST (or per logical scope), shared within that single request but recreated for the next; Transient creates a BRAND NEW instance every single time the service is requested/injected, even multiple times within the same request.
builder.Services.AddSingleton<ICacheService, CacheService>(); // one instance, app-wide
builder.Services.AddScoped<IOrderService, OrderService>(); // one instance per HTTP request
builder.Services.AddTransient<IEmailBuilder, EmailBuilder>(); // new instance every injectionReal-world example Registering a shared in-memory cache as Singleton, a per-request database context as Scoped, and a lightweight stateless helper as Transient.