BackgroundService Class

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Base class for implementing a long running IHostedService.

C#
public abstract class BackgroundService : IDisposable, Microsoft.Extensions.Hosting.IHostedService
Inheritance
BackgroundService
Implements

Constructors

BackgroundService()

Initializes a new instance of the BackgroundService class.

Properties

ExecuteTask

Gets the Task that executes the background operation.

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

ExecuteAsync(CancellationToken)

This method is called when the IHostedService starts. The implementation should return a task that represents the lifetime of the long running operation(s) being performed.

StartAsync(CancellationToken)

Triggered when the application host is ready to start the service.

StopAsync(CancellationToken)

Triggered when the application host is performing a graceful shutdown.

Extension Methods

StartAndStopAsync(IHostedService, CancellationToken)

Starts and immediately stops the service.

Applies to

产品 版本
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Standard 2.0 (package-provided), 2.1 (package-provided)