diff --git a/docs/core/extensions/dependency-injection.md b/docs/core/extensions/dependency-injection.md index 14f710d912ea0..62b3dee84cf4f 100644 --- a/docs/core/extensions/dependency-injection.md +++ b/docs/core/extensions/dependency-injection.md @@ -248,13 +248,19 @@ For web applications, a scoped lifetime indicates that services are created once In apps that process requests, scoped services are disposed at the end of the request. -When using Entity Framework Core, the extension method registers `DbContext` types with a scoped lifetime by default. - > [!NOTE] -> Do ***not*** resolve a scoped service from a singleton and be careful not to do so indirectly, for example, through a transient service. It may cause the service to have incorrect state when processing subsequent requests. It's fine to: -> -> - Resolve a singleton service from a scoped or transient service. -> - Resolve a scoped service from another scoped or transient service. +> When using Entity Framework Core, the extension method registers `DbContext` types with a scoped lifetime by default. + +A scoped service should always be used from within a scope—either an implicit scope (such as ASP.NET Core's per-request scope) or an explicit scope created with . + +Do ***not*** resolve a scoped service directly from a singleton using constructor injection or by requesting it from in the singleton. Doing so causes the scoped service to behave like a singleton, which can lead to incorrect state when processing subsequent requests. + +It's acceptable to resolve a scoped service within a singleton if you create and use an explicit scope with . + +It's also fine to: + +- Resolve a singleton service from a scoped or transient service. +- Resolve a scoped service from another scoped or transient service. By default, in the development environment, resolving a service from another service with a longer lifetime throws an exception. For more information, see [Scope validation](#scope-validation). diff --git a/docs/core/extensions/scoped-service.md b/docs/core/extensions/scoped-service.md index 8a8bc48818da1..dfa46e3f00be9 100644 --- a/docs/core/extensions/scoped-service.md +++ b/docs/core/extensions/scoped-service.md @@ -3,7 +3,7 @@ title: Use scoped services within a BackgroundService description: Learn how to use scoped services within a BackgroundService in .NET. author: IEvangelist ms.author: dapine -ms.date: 11/06/2024 +ms.date: 05/27/2025 ms.topic: tutorial --- @@ -15,7 +15,7 @@ In this tutorial, you learn how to: > [!div class="checklist"] > -> - Resolve scoped dependencies in a singleton . +> - Correctly resolve scoped dependencies in a singleton . > - Delegate work to a scoped service. > - Implement an `override` of .