Table of Contents

Class DistributedBackgroundService

Namespace
Silverback.Background
Assembly
Silverback.Core.dll

Extends the BackgroundService adding a distributed lock mechanism to prevent concurrent executions.

public abstract class DistributedBackgroundService : BackgroundService, IHostedService, IDisposable
Inheritance
DistributedBackgroundService
Implements
Derived
Inherited Members

Constructors

DistributedBackgroundService(IDistributedLock, ISilverbackLogger<DistributedBackgroundService>)

Initializes a new instance of the DistributedBackgroundService class.

protected DistributedBackgroundService(IDistributedLock distributedLock, ISilverbackLogger<DistributedBackgroundService> logger)

Parameters

distributedLock IDistributedLock

The IDistributedLock.

logger ISilverbackLogger<DistributedBackgroundService>

The ISilverbackLogger.

Properties

DistributedLock

Gets the IDistributedLock used by this service.

protected IDistributedLock DistributedLock { get; }

Property Value

IDistributedLock

Methods

AcquireLockAndExecuteAsync(CancellationToken)

Acquires the lock and calls the ExecuteLockedAsync(CancellationToken) method to perform the operation(s).

protected virtual Task AcquireLockAndExecuteAsync(CancellationToken stoppingToken)

Parameters

stoppingToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

Task

A Task representing the asynchronous operation.

ExecuteAsync(CancellationToken)

protected override sealed Task ExecuteAsync(CancellationToken stoppingToken)

Parameters

stoppingToken CancellationToken

Returns

Task

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.

protected abstract Task ExecuteLockedAsync(CancellationToken stoppingToken)

Parameters

stoppingToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

Task

A Task representing the asynchronous operation.