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 toObjectDisposedException
. - π¦ Proper cleanup is done at the end of the request for scoped services.
- π« No need to dispose injected
IDisposable
services likeDbContext
,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!