Verified Jobs Live
.NET Medium Level ~3 min read

What is the difference between singleton, scoped, and transient dependency lifetimes?

Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Model Answer & Deep-Dive Explanation

A singleton instance is reused for the service container’s lifetime. A scoped service is reused within a scope, commonly one web request. A transient is created on each resolution. Choose according to ownership, state, thread safety, and cleanup requirements. A singleton must not capture a shorter-lived scoped dependency in a way that extends its intended lifetime. In ASP.NET Core, database contexts are commonly scoped rather than shared singletons. Registration alone does not make a mutable singleton thread-safe, so test concurrent behavior where it matters.