Skip to content

Commit 4acbe1e

Browse files
authored
Fixes #45617 (#46459)
1 parent 4b5b141 commit 4acbe1e

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

docs/core/extensions/dependency-injection.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,19 @@ For web applications, a scoped lifetime indicates that services are created once
248248

249249
In apps that process requests, scoped services are disposed at the end of the request.
250250

251-
When using Entity Framework Core, the <xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext%2A> extension method registers `DbContext` types with a scoped lifetime by default.
252-
253251
> [!NOTE]
254-
> 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:
255-
>
256-
> - Resolve a singleton service from a scoped or transient service.
257-
> - Resolve a scoped service from another scoped or transient service.
252+
> When using Entity Framework Core, the <xref:Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext%2A> extension method registers `DbContext` types with a scoped lifetime by default.
253+
254+
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 <xref:Microsoft.Extensions.DependencyInjection.IServiceScopeFactory.CreateScope?displayProperty=nameWithType>.
255+
256+
Do ***not*** resolve a scoped service directly from a singleton using constructor injection or by requesting it from <xref:System.IServiceProvider> in the singleton. Doing so causes the scoped service to behave like a singleton, which can lead to incorrect state when processing subsequent requests.
257+
258+
It's acceptable to resolve a scoped service within a singleton if you create and use an explicit scope with <xref:Microsoft.Extensions.DependencyInjection.IServiceScopeFactory>.
259+
260+
It's also fine to:
261+
262+
- Resolve a singleton service from a scoped or transient service.
263+
- Resolve a scoped service from another scoped or transient service.
258264

259265
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).
260266

docs/core/extensions/scoped-service.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Use scoped services within a BackgroundService
33
description: Learn how to use scoped services within a BackgroundService in .NET.
44
author: IEvangelist
55
ms.author: dapine
6-
ms.date: 11/06/2024
6+
ms.date: 05/27/2025
77
ms.topic: tutorial
88
---
99

@@ -15,7 +15,7 @@ In this tutorial, you learn how to:
1515

1616
> [!div class="checklist"]
1717
>
18-
> - Resolve scoped dependencies in a singleton <xref:Microsoft.Extensions.Hosting.BackgroundService>.
18+
> - Correctly resolve scoped dependencies in a singleton <xref:Microsoft.Extensions.Hosting.BackgroundService>.
1919
> - Delegate work to a scoped service.
2020
> - Implement an `override` of <xref:Microsoft.Extensions.Hosting.BackgroundService.StopAsync(System.Threading.CancellationToken)?displayProperty=nameWithType>.
2121

0 commit comments

Comments
 (0)