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 …


This content originally appeared on DEV Community and was authored by Oğuzhan Ağır

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! 👇


This content originally appeared on DEV Community and was authored by Oğuzhan Ağır


Print Share Comment Cite Upload Translate Updates
APA

Oğuzhan Ağır | Sciencx (2025-10-23T06:19:24+00:00) ASP.NET Core Dependency Injection From Scopes & Lifetimes to .NET 9 Source Generators. Retrieved from https://www.scien.cx/2025/10/23/asp-net-core-dependency-injection-from-scopes-lifetimes-to-net-9-source-generators/

MLA
" » ASP.NET Core Dependency Injection From Scopes & Lifetimes to .NET 9 Source Generators." Oğuzhan Ağır | Sciencx - Thursday October 23, 2025, https://www.scien.cx/2025/10/23/asp-net-core-dependency-injection-from-scopes-lifetimes-to-net-9-source-generators/
HARVARD
Oğuzhan Ağır | Sciencx Thursday October 23, 2025 » ASP.NET Core Dependency Injection From Scopes & Lifetimes to .NET 9 Source Generators., viewed ,<https://www.scien.cx/2025/10/23/asp-net-core-dependency-injection-from-scopes-lifetimes-to-net-9-source-generators/>
VANCOUVER
Oğuzhan Ağır | Sciencx - » ASP.NET Core Dependency Injection From Scopes & Lifetimes to .NET 9 Source Generators. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/23/asp-net-core-dependency-injection-from-scopes-lifetimes-to-net-9-source-generators/
CHICAGO
" » ASP.NET Core Dependency Injection From Scopes & Lifetimes to .NET 9 Source Generators." Oğuzhan Ağır | Sciencx - Accessed . https://www.scien.cx/2025/10/23/asp-net-core-dependency-injection-from-scopes-lifetimes-to-net-9-source-generators/
IEEE
" » ASP.NET Core Dependency Injection From Scopes & Lifetimes to .NET 9 Source Generators." Oğuzhan Ağır | Sciencx [Online]. Available: https://www.scien.cx/2025/10/23/asp-net-core-dependency-injection-from-scopes-lifetimes-to-net-9-source-generators/. [Accessed: ]
rf:citation
» ASP.NET Core Dependency Injection From Scopes & Lifetimes to .NET 9 Source Generators | Oğuzhan Ağır | Sciencx | https://www.scien.cx/2025/10/23/asp-net-core-dependency-injection-from-scopes-lifetimes-to-net-9-source-generators/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.