Skip to content

Commit 7957045

Browse files
authored
Document breaking change for BackgroundService ExecuteAsync behavior in .NET 10 (#49121)
1 parent 856fdd4 commit 7957045

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

docs/core/compatibility/10.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ If you're migrating an app to .NET 10, the breaking changes listed here might af
7676

7777
| Title | Type of change | Introduced version |
7878
|-------|---------------------|--------------------|
79+
| [BackgroundService runs all of ExecuteAsync as a Task](extensions/10.0/backgroundservice-executeasync-task.md) | Behavioral change | Preview 1 |
7980
| [Null values preserved in configuration](extensions/10.0/configuration-null-values-preserved.md) | Behavioral change | Preview 7 |
8081
| [Message no longer duplicated in Console log output](extensions/10.0/console-json-logging-duplicate-messages.md) | Behavioral change | Preview 7 |
8182
| [ProviderAliasAttribute moved to Microsoft.Extensions.Logging.Abstractions assembly](extensions/10.0/provideraliasattribute-moved-assembly.md) | Source incompatible | Preview 4 |
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: "Breaking change: BackgroundService runs all of ExecuteAsync as a Task"
3+
description: "Learn about the breaking change in .NET 10 where BackgroundService now runs the entirety of ExecuteAsync on a background thread instead of running the synchronous portion on the main thread."
4+
ms.date: 10/13/2025
5+
ai-usage: ai-assisted
6+
ms.custom: https://github.com/dotnet/runtime/issues/116181
7+
---
8+
9+
# BackgroundService runs all of ExecuteAsync as a Task
10+
11+
<xref:Microsoft.Extensions.Hosting.BackgroundService> now runs the entirety of <xref:Microsoft.Extensions.Hosting.BackgroundService.ExecuteAsync%2A> on a background thread. Previously, the synchronous portion of `ExecuteAsync` (before the first `await`) ran on the main thread during service startup, blocking other services from starting. Only code after the first `await` ran on a background thread.
12+
13+
## Version introduced
14+
15+
.NET 10
16+
17+
## Previous behavior
18+
19+
Previously, the synchronous portion of `ExecuteAsync` ran on the main thread and blocked other services from starting.
20+
21+
## New behavior
22+
23+
Starting in .NET 10, all of `ExecuteAsync` runs on a background thread, and no part of it blocks other services from starting.
24+
25+
## Type of breaking change
26+
27+
This change is a [behavioral change](../../categories.md#behavioral-change).
28+
29+
## Reason for change
30+
31+
The previous behavior was a common pitfall that didn't meet user expectations. Most implementers of `BackgroundService` didn't understand that the synchronous portion before the first `await` blocked other services from starting during application startup.
32+
33+
## Recommended action
34+
35+
If you require any part of your `BackgroundService.ExecuteAsync` to run earlier during startup (synchronously and blocking other services), you can do any one of the following:
36+
37+
- Place the code that needs to run synchronously in the constructor, and it executes as part of the service construction.
38+
- Override `StartAsync` and do some work before calling `base.StartAsync`. `StartAsync` retains the behavior that its synchronous portion runs synchronously during startup and blocks other services from starting.
39+
- If you want to run code at a more specific time during service startup, implement <xref:Microsoft.Extensions.Hosting.IHostedLifecycleService?displayProperty=fullName> on your `BackgroundService`.
40+
- Forgo `BackgroundService` entirely and implement your own <xref:Microsoft.Extensions.Hosting.IHostedService?displayProperty=fullName>.
41+
42+
## Affected APIs
43+
44+
- <xref:Microsoft.Extensions.Hosting.BackgroundService.ExecuteAsync%2A?displayProperty=fullName>

docs/core/compatibility/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ items:
8686
href: /ef/core/what-is-new/ef-core-10.0/breaking-changes?toc=/dotnet/core/compatibility/toc.json&bc=/dotnet/breadcrumb/toc.json
8787
- name: Extensions
8888
items:
89+
- name: BackgroundService runs all of ExecuteAsync as a Task
90+
href: extensions/10.0/backgroundservice-executeasync-task.md
8991
- name: Message no longer duplicated in Console log output
9092
href: extensions/10.0/console-json-logging-duplicate-messages.md
9193
- name: Null values preserved in configuration

0 commit comments

Comments
 (0)