Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ScopedBackgroundService #38880

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/core/extensions/scoped-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Replace the existing `Worker` class with the following C# code, and rename the f

:::code source="snippets/workers/scoped-service/ScopedBackgroundService.cs" highlight="22-28":::

In the preceding code, an explicit scope is created and the `IScopedProcessingService` implementation is resolved from the dependency injection service provider. The resolved service instance is scoped, and its `DoWorkAsync` method is awaited.
In the preceding code, an explicit scope is created and the `IScopedProcessingService` implementation is resolved from the dependency injection service scope factory. The resolved service instance is scoped, and its `DoWorkAsync` method is awaited.

Replace the template *Program.cs* file contents with the following C# code:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace App.ScopedService;

public sealed class ScopedBackgroundService(
IServiceProvider serviceProvider,
IServiceScopeFactory serviceScopeFactory,
ILogger<ScopedBackgroundService> logger) : BackgroundService
{
private const string ClassName = nameof(ScopedBackgroundService);
Expand All @@ -19,7 +19,7 @@ private async Task DoWorkAsync(CancellationToken stoppingToken)
logger.LogInformation(
"{Name} is working.", ClassName);

using (IServiceScope scope = serviceProvider.CreateScope())
using (IServiceScope scope = serviceScopeFactory.CreateScope())
{
IScopedProcessingService scopedProcessingService =
scope.ServiceProvider.GetRequiredService<IScopedProcessingService>();
Expand Down
Loading