DependencyInjection.Lifetime.Analyzers
3.9.2
See the version list below for details.
dotnet add package DependencyInjection.Lifetime.Analyzers --version 3.9.2
NuGet\Install-Package DependencyInjection.Lifetime.Analyzers -Version 3.9.2
<PackageReference Include="DependencyInjection.Lifetime.Analyzers" Version="3.9.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="DependencyInjection.Lifetime.Analyzers" Version="3.9.2" />
<PackageReference Include="DependencyInjection.Lifetime.Analyzers"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add DependencyInjection.Lifetime.Analyzers --version 3.9.2
#r "nuget: DependencyInjection.Lifetime.Analyzers, 3.9.2"
#:package DependencyInjection.Lifetime.Analyzers@3.9.2
#addin nuget:?package=DependencyInjection.Lifetime.Analyzers&version=3.9.2
#tool nuget:?package=DependencyInjection.Lifetime.Analyzers&version=3.9.2
<p align="center"> <img src="https://raw.githubusercontent.com/georgepwall1991/DependencyInjection.Lifetime.Analyzers/main/icon.png" width="96" height="96" alt="DependencyInjection.Lifetime.Analyzers icon"> </p>
DependencyInjection.Lifetime.Analyzers
Catch dependency injection lifetime bugs at compile time — before they become ObjectDisposedException, stale singletons, or production-only activation failures.
A Roslyn analyzer for Microsoft.Extensions.DependencyInjection in ASP.NET Core, worker services, the generic host, and .NET libraries. It adds no runtime dependency and stays quiet when a bug cannot be proven from the compilation.
Rule catalog · Problem guides · Scanner · GitHub
Install
dotnet add package DependencyInjection.Lifetime.Analyzers
<PackageReference Include="DependencyInjection.Lifetime.Analyzers" Version="*">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
No runtime package is added to your app. The analyzer runs in Visual Studio, Rider, and dotnet build / CI.
What it catches
- Captive dependencies — a singleton captures a scoped or transient service (
DI003,DI009for open generics). - Scope leaks and disposal — undisposed
IServiceScope, root providers, use-after-dispose, async scope misuse (DI001,DI004,DI005,DI014). - Scope escape and root resolution — scoped services leaving their scope or resolved from the root provider (
DI002,DI019). - Activation failures — missing registrations, implementation mismatches, non-instantiable types, circular graphs (
DI013,DI015,DI017,DI018). BuildServiceProvider()misuse during composition (DI016).- Service locator drift — static provider caches and direct
IServiceProvideruse (DI006,DI007,DI011). - Host and ASP.NET Core hazards — middleware scoped capture, hosted-service scope-per-iteration,
HttpClientlifetime, fire-and-forget scope/HttpContextcapture (DI020,DI023,DI024,DI029,DI034). - Concurrency and leak families — shared non-thread-safe services, event/Rx/callback subscription leaks, unbounded singleton caches (
DI021–DI022,DI025–DI028,DI030,DI035).
38 shipped rules. Many include code fixes. See the full rule catalog for severity, bad and safe examples, detection boundaries, and suppression.
30-second example
services.AddScoped<IScopedService, ScopedService>();
services.AddSingleton<ISingletonService, SingletonService>();
public sealed class SingletonService : ISingletonService
{
// DI003: the scoped service is captured for the process lifetime.
public SingletonService(IScopedService scoped) { }
}
The safe shape is to make the consumer scoped, or inject IServiceScopeFactory and create a scope per operation.
Try it on a real repository first
di-scan builds any .NET solution with these analyzers injected — without editing its project files — and reports ruleId path:line:column, the surrounding source, SARIF, Markdown, and HTML.
dnx DependencyInjection.Lifetime.Analyzers.Scan -- MyApp.slnx
dotnet tool install -g DependencyInjection.Lifetime.Analyzers.Scan
di-scan MyApp.slnx --fail-on warning --sarif di.sarif
Use --baseline to fail only on new findings. See the scanner guide for output formats, CI examples, and baselines.
Starter profiles
The package ships four .editorconfig profiles under profiles/:
| Profile | Use it when |
|---|---|
default.editorconfig |
You want each rule at its shipped default severity. |
brownfield-adoption.editorconfig |
You are adding the analyzer to a large existing codebase and want composition failures first. |
strict-ci.editorconfig |
Every high-confidence rule should fail CI; informational rules stay warnings. |
library-author.editorconfig |
You ship a library and want activation and composition failures to be errors. |
Why it stays quiet
- Framework types are resolved by symbol, never by type name alone.
- Custom APIs that reuse DI names are not treated as
Microsoft.Extensions.DependencyInjectionevidence. - Test code is suppressed using test project names, directories, type/namespace names, base types, and test attributes.
- When the analyzer cannot prove a bug from registrations, constructors, and scope usage, it does not report.
Proof, not just a rule list
- 3,128 tests
- 14 pinned public repositories and 471 findings in the scan regression corpus
- 95.10% mutation score on the shared test-code suppression helper, with an 80% gate
- Precision fixes driven by real repositories, including MassTransit, Elsa, IdentityServer, Polly, and dotnet-podcasts
The corpus and triage notes are in docs/scan-regression.md.
Compatibility
- Target: projects using
Microsoft.Extensions.DependencyInjection— ASP.NET Core, generic host, workers, console apps, and libraries. - Analyzer host: Roslyn in Visual Studio, Rider, and
dotnet build/ CI. - Package TFM:
netstandard2.0analyzer assembly. - Language: C#.
- Not claimed: third-party containers unless they use the same registration APIs in a way the analyzer can see.
Links
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on DependencyInjection.Lifetime.Analyzers:
| Repository | Stars |
|---|---|
|
VahidN/DNTCommon.Web.Core
DNTCommon.Web.Core provides common scenarios' solutions for ASP.NET Core applications.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 3.10.1 | 496 | 10/27/2026 |
| 3.10.0 | 112 | 10/27/2026 |
| 3.9.3 | 104 | 10/27/2026 |
| 3.9.2 | 97 | 10/27/2026 |
| 3.9.1 | 104 | 10/27/2026 |
| 3.9.0 | 103 | 10/27/2026 |
| 3.7.8 | 10,417 | 8/12/2026 |
| 3.7.7 | 140 | 8/9/2026 |
| 3.7.6 | 125 | 8/9/2026 |
| 3.7.5 | 121 | 8/9/2026 |
| 3.7.4 | 136 | 8/9/2026 |
| 3.7.3 | 128 | 8/9/2026 |
| 3.7.2 | 136 | 8/9/2026 |
| 3.7.1 | 130 | 8/9/2026 |
| 3.7.0 | 174 | 7/31/2026 |
| 3.6.0 | 153 | 7/31/2026 |
| 3.5.20 | 151 | 7/27/2026 |
| 3.5.19 | 143 | 7/27/2026 |
| 3.5.18 | 131 | 7/27/2026 |
| 3.5.17 | 149 | 7/27/2026 |
NuGet search polish for the scanner: the package title, description, tags, and project URL now use the exact `di-scan` name and link to the scanner guide. | The analyzer description now names the dependency injection lifetime analyzer explicitly while keeping the high-intent lifetime, captive-dependency, scope-leak, and BuildServiceProvider terms. | The README scanner section links to the scanner NuGet package.