Problem guide

Fix ObjectDisposedException From Scoped Services

These diagnostics catch the most common scope-lifetime mistakes that lead to disposed-service failures in background jobs, middleware, and startup code.

When this page is relevant

Compile-time guidance for scope leaks, services escaping scopes, and using resolved services after the scope ends in ASP.NET Core and .NET apps.

Recommended install command

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

Relevant diagnostics

The rules that cover this failure mode

DI001

Service Scope Not Disposed

`IServiceScope` instances created with `CreateScope()` or `CreateAsyncScope()` that are never disposed.

Severity: Warning · Code fix: Yes

DI002

Scoped Service Escapes Scope

a service resolved from a scope that is returned or stored somewhere longer-lived.

Severity: Warning · Code fix: Yes