Problem guide

Reduce IServiceProvider Injection And Service Locator Usage

Use these diagnostics when you want a stronger constructor-injection culture across app code and shared libraries.

When this page is relevant

Surface hidden dependencies, service locator drift, and direct IServiceProvider injection in application code before it hardens into architecture debt.

Recommended install command

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

Relevant diagnostics

The rules that cover this failure mode

DI006

Static `IServiceProvider` Cache

`IServiceProvider` / `IServiceScopeFactory` / keyed provider stored in static fields or properties, including common wrappers (`Lazy<T>`, `Task<T>`, `ValueTask<T>`, `Func<T>`, `AsyncLocal<T>`, `ThreadLocal<T>`), mutable/immutable/frozen dictionary value caches, recursive dictionary values such as `Dictionary<string, Lazy<IServiceProvider>>`, and simple holder types that only wrap a provider. Static fields initialized with a custom, stateless `IServiceProvider` implementation (no instance fields or properties) are null-object providers and stay quiet; framework providers and custom providers with any instance state still report.

Severity: Warning · Code fix: Yes

DI007

Service Locator Anti-Pattern

Resolving dependencies via `IServiceProvider` inside app logic, including non-generic resolution calls that pass a local `Type` alias initialized from `typeof(...)`.

Severity: Info · Code fix: No

DI011

`IServiceProvider` Injection

Constructor injection of `IServiceProvider`, `IServiceScopeFactory`, or `IKeyedServiceProvider` in normal services.

Severity: Info · Code fix: No