DEV Community

Cover image for ASP.NET Core Dependency Injection From Scopes & Lifetimes to .NET 9 Source Generators
Oğuzhan Ağır
Oğuzhan Ağır

Posted on

ASP.NET Core Dependency Injection From Scopes & Lifetimes to .NET 9 Source Generators

Dependency Injection in ASP.NET Core is simple... until you hit a subtle memory leak or a captive dependency bug.

Are you positive you're not creating a "captive dependency"? Do you know the right way to use a Scoped service (like a DbContext) inside a Singleton background worker?

I've just published a guide that goes beyond the basics and dives into the advanced patterns and new features you need to know.

This isn't just AddTransient vs. AddScoped again. We cover:

  • The Captive Dependency trap (and how to fix it).
  • Using IServiceScopeFactory in IHostedService like a pro.
  • .NET 9 Source Generators for blazing fast startup & Native AOT.
  • Keyed Services ([FromKeyedServices("key")]) for runtime flexibility.
  • The Decorator Pattern for clean, cross-cutting concerns.
  • Proper async cleanup with IAsyncDisposable.

You can move beyond the basics and start writing robust, testable, and high performing DI compatible code. This guide shows you how.

👉 Read the Full Guide on ABP.io

What's your favorite Dependency Injection hack? Let me know in the comments! 👇

Top comments (0)