AsyncHostedService Class
Subclasses AsyncService, providing support for logging and implementing IHostedService.
public abstract class AsyncHostedService : AsyncService, IHostedService- Inheritance
AsyncServiceAsyncHostedService- Implements
Constructors
| Name | Description |
|---|---|
| AsyncHostedService(ILogger) | Initializes a new instance of the AsyncHostedService class. |
AsyncHostedService(ILogger)
Initializes a new instance of the AsyncHostedService class.
protected AsyncHostedService(ILogger logger)Properties
| Name | Description |
|---|---|
| FailOnSetupNotStarted | Gets a value indicating whether AsyncHostedService.StartAsync will fail if the result of AsyncService.StartAndWaitAsync is AsyncServiceSetupResult.NotStarted. |
| FailOnSetupUnsuccessful | Gets 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
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
Methods
| Name | Description |
|---|---|
| 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)
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.
Parameters
successbooltrueif the operation was successful;falseotherwise.
LogSetupFailed(Exception)
LogStateChanged(AsyncServiceState, AsyncServiceState)
Message: Service state {oldState} -> {newState}
Level: Trace
protected sealed override void LogStateChanged(AsyncServiceState oldState, AsyncServiceState newState)Parameters
oldStateAsyncServiceStatenewStateAsyncServiceState
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
previousStateAsyncServiceState- The value of the
AsyncService.Stateproperty before the stop request. currentStateAsyncServiceState- The value of the
AsyncService.Stateproperty after the stop request. resultbooltrueif the service was running when requested to stop;falseotherwise.
LogTeardownCompleted()
Message: Teardown phase completed
Level: Trace
protected sealed override void LogTeardownCompleted()LogTeardownFailed(Exception)
StartAsync(CancellationToken)
Triggered when the application host is ready to start the service.
public Task StartAsync(CancellationToken cancellationToken)Parameters
cancellationTokenCancellationToken- 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.StartAndWaitAsyncisAsyncServiceSetupResult.NotStartedandAsyncHostedService.FailOnSetupNotStartedistrue. The result ofAsyncService.StartAndWaitAsyncisAsyncServiceSetupResult.UnsuccessfulandAsyncHostedService.FailOnSetupUnsuccessfulistrue. OperationCanceledException- The setup phase was canceled, either through
cancellationTokenor 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
cancellationTokenCancellationToken- 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.