Dependency InjectionSenior

🐞 Injecting Scoped into Singleton

Why is this dangerous?

builder.Services.AddSingleton<MyService>();

public class MyService
{
    public MyService(AppDbContext db)
    {

    }
}

✅ Explanation

  • AppDbContext is Scoped.
  • Singleton lives for the application's lifetime.
  • This can cause disposed object issues and thread safety problems.

💡 Senior Interview Tip

Don't stop at identifying the bug. Explain why it happens, its impact on production, and how you would fix it. That's what interviewers expect from mid-level and senior developers.