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 3.7.8

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 — including a provider stored in a local and returned later (ownership transfer), and create-and-dispose within the same loop iteration, switch section, or catch clause (a `continue`/`break` that skips the dispose still reports). A `using` declaration or statement proves cleanup only when that same provider instance reaches its resource expression. User-defined conversions remain reportable because they may produce a different instance, including a disposable wrapper selected by a coalesce inside `using`.

Severity: Warning · Code fix: Yes