DEV Community

Sachin Ghatage
Sachin Ghatage

Posted on

What happens when you inject scoped dependency into singleton?

leads to thread-safety issues and memory leaks due to EF’s Change Tracker.
It will give InvalidOperationException.
You registered your DbContext as Scoped → one instance per HTTP request.

You registered your repository/service as Singleton → one instance for the entire application lifetime.

Your Singleton service depends on Scoped DbContext.

Why this is dangerous

Even if somehow you delayed instantiation and it works:

You might accidentally share the same DbContext across multiple requests.

This causes thread-safety issues, stale data, and memory leaks

Top comments (0)