AsyncHostedService Class

Subclasses AsyncService, providing support for logging and implementing IHostedService.

public abstract class AsyncHostedService : AsyncService, IHostedService
Inheritance
AsyncService AsyncHostedService
Implements

Constructors

NameDescription
AsyncHostedService(ILogger)Initializes a new instance of the AsyncHostedService class.

AsyncHostedService(ILogger)

Initializes a new instance of the AsyncHostedService class.

protected AsyncHostedService(ILogger logger)

Parameters

logger ILogger
An ILogger to use for logging.

Properties

NameDescription
FailOnSetupNotStartedGets a value indicating whether AsyncHostedService.StartAsync will fail if the result of AsyncService.StartAndWaitAsync is AsyncServiceSetupResult.NotStarted.
FailOnSetupUnsuccessfulGets a value indicating whether AsyncHostedService.StartAsync will fail if the result of AsyncService.StartAndWaitAsync is AsyncServiceSetupResult.Unsuccessful.

FailOnSetupNotStarted

Gets a value indicating whether AsyncHostedService.StartAsync will fail if the result of AsyncService.StartAndWaitAsync is AsyncServiceSetupResult.NotStarted.

protected virtual bool FailOnSetupNotStarted { get; }

Property value

bool

FailOnSetupUnsuccessful

Gets a value indicating whether AsyncHostedService.StartAsync will fail if the result of AsyncService.StartAndWaitAsync is AsyncServiceSetupResult.Unsuccessful.

protected virtual bool FailOnSetupUnsuccessful { get; }

Property value

bool

Methods

NameDescription
LogBeforeExecute()Message: Starting service execution Level: Trace
LogBeforeSetup()Message: Starting setup phase Level: Trace
LogBeforeTeardown()Message: Starting teardown phase Level: Trace
LogExecuteCanceled()Message: Service execution was canceled Level: Warning
LogExecuteCompleted()Message: Service execution completed Level: Trace
LogExecuteFailed(Exception)Message: Service execution failed Level: Error
LogSetupCanceled()Message: Service execution was canceled during setup phase Level: Warning
LogSetupCompleted(bool)Called if the AsyncService.SetupAsync method terminates successfully. This method must return as early as possible, must not throw, and should be only used for logging purposes.
LogSetupFailed(Exception)Message: Service setup phase failed Level: Error
LogStateChanged(AsyncServiceState, AsyncServiceState)Message: Service state {oldState} -> {newState} Level: Trace
LogStopRequested(AsyncServiceState, AsyncServiceState, bool)Called upon requesting that a service stops. This method must return as early as possible, must not throw, and should be only used for logging purposes.
LogTeardownCompleted()Message: Teardown phase completed Level: Trace
LogTeardownFailed(Exception)Message: Service teardown phase failed Level: Error
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.

LogBeforeExecute()

Message: Starting service execution

Level: Trace

protected sealed override void LogBeforeExecute()

LogBeforeSetup()

Message: Starting setup phase

Level: Trace

protected sealed override void LogBeforeSetup()

LogBeforeTeardown()

Message: Starting teardown phase

Level: Trace

protected sealed override void LogBeforeTeardown()

LogExecuteCanceled()

Message: Service execution was canceled

Level: Warning

protected sealed override void LogExecuteCanceled()

LogExecuteCompleted()

Message: Service execution completed

Level: Trace

protected sealed override void LogExecuteCompleted()

LogExecuteFailed(Exception)

Message: Service execution failed

Level: Error

protected sealed override void LogExecuteFailed(Exception exception)

Parameters

exception Exception

LogSetupCanceled()

Message: Service execution was canceled during setup phase

Level: Warning

protected sealed override void LogSetupCanceled()

LogSetupCompleted(bool)

Called if the AsyncService.SetupAsync method terminates successfully.

This method must return as early as possible, must not throw, and should be only used for logging purposes.

protected sealed override void LogSetupCompleted(bool success)

Parameters

success bool
true if the operation was successful; false otherwise.

LogSetupFailed(Exception)

Message: Service setup phase failed

Level: Error

protected sealed override void LogSetupFailed(Exception exception)

Parameters

exception Exception

LogStateChanged(AsyncServiceState, AsyncServiceState)

Message: Service state {oldState} -> {newState}

Level: Trace

protected sealed override void LogStateChanged(AsyncServiceState oldState, AsyncServiceState newState)

Parameters

oldState AsyncServiceState
newState AsyncServiceState

LogStopRequested(AsyncServiceState, AsyncServiceState, bool)

Called upon requesting that a service stops.

This method must return as early as possible, must not throw, and should be only used for logging purposes.

protected sealed override void LogStopRequested(AsyncServiceState previousState, AsyncServiceState currentState, bool result)

Parameters

previousState AsyncServiceState
The value of the AsyncService.State property before the stop request.
currentState AsyncServiceState
The value of the AsyncService.State property after the stop request.
result bool
true if the service was running when requested to stop; false otherwise.

LogTeardownCompleted()

Message: Teardown phase completed

Level: Trace

protected sealed override void LogTeardownCompleted()

LogTeardownFailed(Exception)

Message: Service teardown phase failed

Level: Error

protected sealed override void LogTeardownFailed(Exception exception)

Parameters

exception Exception

StartAsync(CancellationToken)

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

public Task StartAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken
Indicates that the start process has been aborted.

Returns

Task A Task that represents the asynchronous Start operation.

Exceptions

InvalidOperationException
The service has already been started. The result of AsyncService.StartAndWaitAsync is AsyncServiceSetupResult.NotStarted and AsyncHostedService.FailOnSetupNotStarted is true. The result of AsyncService.StartAndWaitAsync is AsyncServiceSetupResult.Unsuccessful and AsyncHostedService.FailOnSetupUnsuccessful is true.
OperationCanceledException
The setup phase was canceled, either through cancellationToken or by a stop request.

Remarks

If AsyncService.SetupAsync throws an exception, this method throws the same exception.

StopAsync(CancellationToken)

Triggered when the application host is performing a graceful shutdown.

public Task StopAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken
Indicates that the shutdown process should no longer be graceful.

Returns

Task A Task that represents the asynchronous Stop operation.

Remarks

This method requests the service to stop, then waits until either the service has stopped or cancellationToken is canceled, whichever comes first.