Service Lifetimes in .NET Core (Transient vs Scoped vs Singleton)

๐Ÿ’ก Concept Name

Service Lifetimes in Dependency Injection

๐Ÿ“˜ Quick Intro

.NET Core offers three types of service lifetimes for Dependency Injection: Transient, Scoped, and Singleton. Each controls how long an instance of a service lives.

๐Ÿง  Analogy / Short Story

Imagine a water bottle:

  • Transient: A new bottle every time you ask.
  • Scoped: One bottle per trip to the water cooler.
  • Singleton: One bottle shared by everyone forever.

๐Ÿ”ง Technical Explanation

  • Transient: A new instance every time it's requested.
  • Scoped: A new instance per HTTP request (per scope).
  • Singleton: A single shared instance for the entire application lifetime.

๐ŸŽฏ Purpose & Use Case

  • โœ… Transient: Use when the service has no state or is cheap to create.
  • โœ… Scoped: Use for services that need to track request-specific data.
  • โœ… Singleton: Use for shared services or global state like cache, config providers.

๐Ÿ’ป Real Code Example

// Registering services
builder.Services.AddTransient<IMyService, MyService>();
builder.Services.AddScoped<IMyService, MyService>();
builder.Services.AddSingleton<IMyService, MyService>();

// Injected into controllers
public class HomeController : Controller
{
    private readonly IMyService _service;
    public HomeController(IMyService service)
    {
        _service = service;
    }
}

โ“ Interview Q&A

Q1: What are the three service lifetimes?
A: Transient, Scoped, Singleton.

Q2: What does Transient mean?
A: A new instance is created every time.

Q3: When is Scoped instance created?
A: Once per HTTP request or scope.

Q4: How long does a Singleton last?
A: For the entire lifetime of the application.

Q5: Which lifetime is ideal for shared cache?
A: Singleton.

Q6: Can Singleton services have constructor dependencies?
A: Yes, but those must also be Singleton or safe to share.

Q7: What happens if Scoped is injected into Singleton?
A: It can cause runtime errors or memory leaks.

Q8: Which method registers a Transient service?
A: AddTransient.

Q9: Which method registers a Singleton service?
A: AddSingleton.

Q10: Which service lifetime is recommended for DB context?
A: Scoped.

๐Ÿ“ MCQs

Q1. Which lifetime creates a new instance each time it's requested?

  • Scoped
  • Singleton
  • Transient
  • Static

Q2. What lifetime provides the same instance within a request?

  • Transient
  • Singleton
  • Scoped
  • Global

Q3. Which lifetime lives for the whole app runtime?

  • Transient
  • Scoped
  • Singleton
  • Thread

Q4. Which method registers a Singleton service?

  • AddScoped
  • AddTransient
  • AddSingleton
  • UseSingleton

Q5. When should you use Scoped services?

  • Always
  • When performance needed
  • When per-request instance is needed
  • To reduce memory

Q6. What happens when a Scoped service is injected into Singleton?

  • Nothing
  • Scoped becomes Singleton
  • It can cause runtime issues
  • It logs a warning only

Q7. Which DI lifetime is least memory efficient if used incorrectly?

  • Scoped
  • Transient
  • Singleton
  • None of these

Q8. Which of these is ideal for stateless lightweight services?

  • Singleton
  • Transient
  • Scoped
  • Static

Q9. Where do you register service lifetimes?

  • Startup.cs Main method
  • Web.config
  • Controller constructor
  • builder.Services in Program.cs

Q10. What’s the correct syntax for Scoped service registration?

  • AddService()
  • AddScoped()
  • RegisterScoped()
  • InjectScoped()

๐Ÿ’ก Bonus Insight

Never inject a Scoped service into a Singleton service. Use interfaces to reduce tight coupling and enable easy lifetime switching.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: