Problem guide

Find Captive Dependencies In ASP.NET Core

This page is for teams searching for captive dependency detection in ASP.NET Core, worker services, and generic-host apps.

When this page is relevant

Use compile-time diagnostics to catch singleton-to-scoped and singleton-to-transient lifetime mismatches before stale state or thread-safety bugs ship.

Recommended install command

dotnet add package DependencyInjection.Lifetime.Analyzers --version 2.8.26

Relevant diagnostics

The rules that cover this failure mode

DI003

Captive Dependency

singleton services capturing scoped or transient dependencies, including constructor injection, `IEnumerable<T>` collection captures, known scoped framework services such as `IOptionsSnapshot<T>`, EF Core contexts and `DbContextOptions<TContext>` registrations from `AddDbContext(...)`, `AddDbContextFactory(...)`, `AddDbContextPool(...)`, and `AddPooledDbContextFactory(...)` including service/implementation overload self-registrations, and high-confidence factory paths such as inline delegates, stable local delegate factories, method-group factories, `GetServices<T>()`, keyed resolutions, and `ActivatorUtilities.CreateInstance(...)` without explicit constructor arguments.

Severity: Warning · Code fix: Yes

DI009

Open Generic Captive Dependency

open generic singleton registrations that depend on shorter-lived services, including `TryAddSingleton(...)`, `ServiceDescriptor.Singleton(...)`, keyed open-generic singleton registrations, and `IEnumerable<T>` constructor captures where the element service is shorter-lived.

Severity: Warning · Code fix: Yes