MediumConcept

What are transient, scoped and singleton service lifetimes in ASP.NET Core dependency injection?

Technology:
C#
Experience:
Mid-level,
Senior

Quick answer

Transient services are created every time they are requested, scoped services are created once per HTTP request, and singleton services are created once and shared for the life of the application.

Detailed explanation

Register services in Program.cs with AddTransient, AddScoped or AddSingleton. Use transient for lightweight, stateless services, scoped for things tied to a request such as an Entity Framework DbContext, and singleton for shared, thread-safe objects such as caches and configuration.

The classic bug is a captive dependency: a singleton that depends on a scoped service keeps that one instance alive forever, so a DbContext ends up shared across requests and threads. ASP.NET Core can detect this in development with scope validation. Singletons must also be thread-safe because many requests use them at once.

Example

csharp
builder.Services.AddTransient<IEmailBuilder, EmailBuilder>();
builder.Services.AddScoped<AppDbContext>();
builder.Services.AddSingleton<IMemoryCache, MemoryCache>();

public class OrdersController(AppDbContext db, IEmailBuilder email) : ControllerBase { }

Key points

  • Transient: new every time. Scoped: per request. Singleton: one for the app
  • Never inject a scoped service into a singleton
  • Singletons must be thread-safe

Follow-up questions

  • What is the middleware pipeline in ASP.NET Core?
  • What is the difference between AddScoped and AddDbContext?
  • C#Easy

    What is the difference between const and readonly in C#?

    A const is a compile-time constant that must be assigned where it is declared, while a readonly field can be assigned at declaration or in the constructor, so its value can be decided at runtime.

    Junior 路 Mid-level 路 Programming Fundamentals
  • C#Medium

    What is LINQ in C# and what is deferred execution?

    LINQ (Language Integrated Query) lets you query collections, databases and XML with a consistent syntax, and deferred execution means a query does not run when you define it but when you enumerate the results.

    Junior 路 Mid-level 路 Functions & Scope