Back to results for “Dependency injection”
.NET MAUI › Dependency Injection & Services

What are the DI service lifetimes and when do you use each?

Beginner

Answer

Singleton creates one instance for the app's lifetime—good for shared state, HTTP clients, and caches. Transient creates a new instance per resolution—good for view models and lightweight services. Scoped is per-scope (used more in Blazor Hybrid). Mismatched lifetimes (a singleton depending on a transient) can cause captive dependencies.
builder.Services.AddSingleton<ISettingsService, SettingsService>();
builder.Services.AddTransient<DetailsViewModel>();
Real-world example A settings service is a singleton so all pages read the same values, while each details view model is transient to avoid stale state between visits.

Common follow-ups: What is a captive dependency? | Which lifetime fits a view model?