Problem guide

Avoid BuildServiceProvider In ConfigureServices

Use these rules when you want to stop BuildServiceProvider misuse during registration while still allowing intentional provider factory methods.

When this page is relevant

Catch BuildServiceProvider misuse during service registration so composition code does not silently create a second service container.

Recommended install command

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

Relevant diagnostics

The rules that cover this failure mode

DI016

BuildServiceProvider Misuse

`BuildServiceProvider()` calls while composing registrations (for example in `ConfigureServices`, `IServiceCollection` extension registration methods, registration lambdas, or builder-style `.Services` helper flows), whether written as reduced extension syntax (`services.BuildServiceProvider()`) or as a direct static call (`ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(builder.Services)`).

Severity: Warning · Code fix: No

DI014

Root Service Provider Not Disposed

root providers from `BuildServiceProvider()` that are never disposed, including local providers whose only manual disposal is conditional, catch-only, after reassignment to another provider, or after repeated creation inside a loop. Straight-line explicit disposal, standard `Dispose()` to `Dispose(true)` cleanup, and caller-owned return flows are accepted even when the `BuildServiceProvider()` result is parenthesized, same-instance cast, null-forgiven, selected by a ternary arm, or supplied by a null-coalescing operand; user-defined conversions remain reportable because they may produce a different instance.

Severity: Warning · Code fix: Yes