Class DistributedBackgroundService
Extends the BackgroundService adding a distributed lock mechanism to prevent concurrent executions.
Inheritance
Inherited Members
Namespace: Silverback.Background
Assembly: Silverback.Core.dll
Syntax
public abstract class DistributedBackgroundService : BackgroundService, IHostedService, IDisposable
Constructors
DistributedBackgroundService(IDistributedLock, ISilverbackLogger<DistributedBackgroundService>)
Initializes a new instance of the DistributedBackgroundService class.
Declaration
protected DistributedBackgroundService(IDistributedLock distributedLock, ISilverbackLogger<DistributedBackgroundService> logger)
Parameters
| Type | Name | Description |
|---|---|---|
| IDistributedLock | distributedLock | The IDistributedLock. |
| ISilverbackLogger<DistributedBackgroundService> | logger | The ISilverbackLogger. |
Properties
DistributedLock
Gets the IDistributedLock used by this service.
Declaration
protected IDistributedLock DistributedLock { get; }
Property Value
| Type | Description |
|---|---|
| IDistributedLock |
Methods
AcquireLockAndExecuteAsync(CancellationToken)
Acquires the lock and calls the ExecuteLockedAsync(CancellationToken) method to perform the operation(s).
Declaration
protected virtual Task AcquireLockAndExecuteAsync(CancellationToken stoppingToken)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | stoppingToken | A CancellationToken to observe while waiting for the task to complete. |
Returns
| Type | Description |
|---|---|
| Task | A Task representing the asynchronous operation. |
ExecuteAsync(CancellationToken)
Extends the BackgroundService adding a distributed lock mechanism to prevent concurrent executions.
Declaration
protected override sealed Task ExecuteAsync(CancellationToken stoppingToken)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | stoppingToken |
Returns
| Type | Description |
|---|---|
| Task |
Overrides
ExecuteLockedAsync(CancellationToken)
This method is called after the lock is acquired to perform the actual operation(s). The implementation should return a task that represents the lifetime of the operation(s) being performed.
Declaration
protected abstract Task ExecuteLockedAsync(CancellationToken stoppingToken)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | stoppingToken | A CancellationToken to observe while waiting for the task to complete. |
Returns
| Type | Description |
|---|---|
| Task | A Task representing the asynchronous operation. |