DependencyInjection.Lifetime.Analyzers 3.10.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DependencyInjection.Lifetime.Analyzers --version 3.10.0
                    
NuGet\Install-Package DependencyInjection.Lifetime.Analyzers -Version 3.10.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DependencyInjection.Lifetime.Analyzers" Version="3.10.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DependencyInjection.Lifetime.Analyzers" Version="3.10.0" />
                    
Directory.Packages.props
<PackageReference Include="DependencyInjection.Lifetime.Analyzers">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DependencyInjection.Lifetime.Analyzers --version 3.10.0
                    
#r "nuget: DependencyInjection.Lifetime.Analyzers, 3.10.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DependencyInjection.Lifetime.Analyzers@3.10.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DependencyInjection.Lifetime.Analyzers&version=3.10.0
                    
Install as a Cake Addin
#tool nuget:?package=DependencyInjection.Lifetime.Analyzers&version=3.10.0
                    
Install as a Cake Tool

<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.

NuGet NuGet Downloads Docs CI License: MIT

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, DI009 for 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 IServiceProvider use (DI006, DI007, DI011).
  • Host and ASP.NET Core hazards — middleware scoped capture, hosted-service scope-per-iteration, HttpClient lifetime, fire-and-forget scope/HttpContext capture (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 --fix to apply the safe code fixes, --update-baseline to adopt existing debt, and --baseline to fail only on new findings. See the scanner guide for output formats, CI examples, and baselines.

DI Scan HTML report showing DI003, DI006, DI009, DI016, DI019, and DI012b findings with source context

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.DependencyInjection evidence.
  • 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,133 tests
  • 14 pinned public repositories and 471 findings in the scan regression corpus
  • Mutation gates at 80% for the test-code suppressor (95.10%) and shared resolution helpers (80.00%)
  • 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.0 analyzer assembly.
  • Language: C#.
  • Not claimed: third-party containers unless they use the same registration APIs in a way the analyzer can see.
There are no supported framework assets in this package.

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
Loading failed

`di-scan --fix` applies the rules' safe code fixes with `dotnet format` before scanning. | `di-scan --update-baseline <file>` writes the current findings as a text baseline; `--baseline` reads it back. | A Windows/macOS CI matrix exercises scanner path handling, package contents, and tool consumption. | Dedicated false-positive and false-negative issue forms, Dependabot updates, and CODEOWNERS.