BackgroundService Class

Definition

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

Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided), 2.1 (package-provided)