Skip to content

Commit a28c16f

Browse files
authored
Update hosted-services.md to reflect BackgroundService.StartAsync breaking change (#37054)
* Update hosted-services.md to reflect BackgroundService.StartAsync breaking change As of dotnet/runtime#116283 (in .NET 10), `BackgroundService.StartAsync` no longer blocks until the `BackgroundService.ExecuteAsync` implementation reaches an `await` point; `StartAsync` now always runs `ExecuteAsync` on the thread pool using `Task.Run`. * Fix updated doc to only apply to >= .NET 10
1 parent 2be7e84 commit a28c16f

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

aspnetcore/fundamentals/host/hosted-services.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,22 @@ The hosted service is activated once at app startup and gracefully shut down at
8080

8181
<xref:Microsoft.Extensions.Hosting.BackgroundService> is a base class for implementing a long running <xref:Microsoft.Extensions.Hosting.IHostedService>.
8282

83+
:::moniker-end
84+
85+
:::moniker range=">= aspnetcore-10.0"
86+
87+
[ExecuteAsync(CancellationToken)](xref:Microsoft.Extensions.Hosting.BackgroundService.ExecuteAsync%2A) is called on the thread pool to run the background service. The implementation returns a <xref:System.Threading.Tasks.Task> that represents the entire lifetime of the background service. The host blocks in [StopAsync(CancellationToken)](xref:Microsoft.Extensions.Hosting.BackgroundService.StopAsync%2A) waiting for `ExecuteAsync` to complete.
88+
89+
:::moniker-end
90+
91+
:::moniker range=">= aspnetcore-8.0 < aspnetcore-10.0"
92+
8393
[ExecuteAsync(CancellationToken)](xref:Microsoft.Extensions.Hosting.BackgroundService.ExecuteAsync%2A) is called to run the background service. The implementation returns a <xref:System.Threading.Tasks.Task> that represents the entire lifetime of the background service. No further services are started until [ExecuteAsync becomes asynchronous](https://github.com/dotnet/extensions/issues/2149), such as by calling `await`. Avoid performing long, blocking initialization work in `ExecuteAsync`. The host blocks in [StopAsync(CancellationToken)](xref:Microsoft.Extensions.Hosting.BackgroundService.StopAsync%2A) waiting for `ExecuteAsync` to complete.
8494

95+
:::moniker-end
96+
97+
:::moniker range=">= aspnetcore-8.0"
98+
8599
The cancellation token is triggered when [IHostedService.StopAsync](xref:Microsoft.Extensions.Hosting.IHostedService.StopAsync%2A) is called. Your implementation of `ExecuteAsync` should finish promptly when the cancellation token is fired in order to gracefully shut down the service. Otherwise, the service ungracefully shuts down at the shutdown timeout. For more information, see the [IHostedService interface](#ihostedservice-interface) section.
86100

87101
For more information, see the [BackgroundService](https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/BackgroundService.cs) source code.

0 commit comments

Comments
 (0)