Advertisement
Beginner .NET MAUI, Dependency Injection & Services

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

Short 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).

Save .NET MAUI mock interview
Advertisement
AModel 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.
Likely follow-ups
  • What is a captive dependency? | Which lifetime fits a view model?
Related topics