Should You Call Dispose on Injected Services?

πŸ’‘ Concept Name

Disposal of Injected Services in ASP.NET Core

πŸ“˜ Quick Intro

In ASP.NET Core, services injected via Dependency Injection (DI) are automatically managed by the framework. You should not manually dispose of them inside your controller or service code.

🧠 Analogy / Short Story

Think of ASP.NET Core’s DI like a hotel housekeeping service. You can enjoy clean towels and sheets, but you don’t have to do the laundry yourself β€” the system takes care of it. Calling Dispose() manually is like tossing clean towels into the trash.

πŸ”§ Technical Explanation

  • πŸ” Services registered with DI can be transient, scoped, or singleton.
  • πŸ›  The DI container tracks disposable services and calls Dispose() when appropriate.
  • ⚠️ Manually calling Dispose() can lead to ObjectDisposedException.
  • πŸ“¦ Proper cleanup is done at the end of the request for scoped services.
  • 🚫 No need to dispose injected IDisposable services like DbContext, HttpClient, etc.

🎯 Purpose & Use Case

  • βœ… Avoid memory leaks by using DI properly.
  • βœ… Let the framework manage lifetimes and cleanup.
  • βœ… Follow best practices for clean, testable controllers and services.

πŸ’» Real Code Example

Don’t do this:

// ❌ Incorrect
public class MyController : ControllerBase {
    private readonly IService _service;
    public MyController(IService service) {
        _service = service;
    }

    public IActionResult Get() {
        _service.Dispose(); // ❌ Don't do this
        return Ok();
    }
}

βœ… Do this instead:

public class MyController : ControllerBase {
    private readonly IService _service;
    public MyController(IService service) {
        _service = service;
    }

    public IActionResult Get() {
        return Ok();
    }
}

❓ Interview Q&A

Q1: Should you dispose injected services?
A: No, the DI container handles it.

Q2: What happens if you call Dispose manually?
A: You may get ObjectDisposedException.

Q3: Who manages the lifecycle of injected services?
A: The built-in ASP.NET Core DI container.

Q4: What lifetime disposes objects at end of request?
A: Scoped lifetime.

Q5: Should you dispose DbContext in controller?
A: No, it's disposed automatically if registered in DI.

πŸ“ MCQs

Q1. Should you call Dispose() on a service injected via DI?

  • Yes
  • No
  • Only if it's a singleton
  • Only in production

Q2. Who manages disposable services in ASP.NET Core?

  • The controller
  • The service class
  • The DI container
  • Middleware

Q3. What can happen if you manually dispose an injected service?

  • Nothing
  • Better performance
  • ObjectDisposedException
  • The app runs faster

Q4. Which service lifetime disposes objects at the end of a request?

  • Transient
  • Singleton
  • Scoped
  • None

Q5. Is it safe to inject IDisposable services?

  • No
  • Yes
  • Only if they are transient
  • Only if you dispose manually

πŸ’‘ Bonus Insight

If you need to control disposal manually, use IServiceScopeFactory and using scopes manually, but this is rarely needed in standard controller scenarios.

πŸ“„ PDF Download

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

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: