Docs
Tenantry ProAPI reference

TenantBackgroundService<TKey> class

Namespace: Tenantry.Pro.BackgroundServices · Package: Tenantry.Pro · API reference

Base class for a hosted service that performs work for every tenant once, then completes. Each tenant is processed inside its own combined DI + tenant scope, and a failure processing one tenant is logged and isolated so it never aborts the sweep of the remaining tenants.

Override TenantBackgroundService<TKey>.ExecuteForTenantAsync with the per-tenant work; resolve scoped services (your DbContext, repositories, …) from the supplied IServiceProvider, which is already bound to the tenant. Register the subclass with builder.Services.AddHostedService<MySweep>(). For recurring work, derive from PeriodicTenantBackgroundService<TKey> instead.

public abstract class TenantBackgroundService<TKey> : BackgroundService, IHostedService, IDisposable where TKey : IEquatable<TKey>, IParsable<TKey>

Type parameters

  • TKey: The tenant identifier type.

Inherits BackgroundService.

Implements IHostedService, IDisposable.

Derived types: PeriodicTenantBackgroundService<TKey>.

Constructors

TenantBackgroundService(ITenantScopeFactory<TKey>, ITenantStoreAccessor<TKey>, ILogger)

Base class for a hosted service that performs work for every tenant once, then completes. Each tenant is processed inside its own combined DI + tenant scope, and a failure processing one tenant is logged and isolated so it never aborts the sweep of the remaining tenants.

protected TenantBackgroundService(ITenantScopeFactory<TKey> scopeFactory, ITenantStoreAccessor<TKey> storeAccessor, ILogger logger)

Parameters:

Override TenantBackgroundService<TKey>.ExecuteForTenantAsync with the per-tenant work; resolve scoped services (your DbContext, repositories, …) from the supplied IServiceProvider, which is already bound to the tenant. Register the subclass with builder.Services.AddHostedService<MySweep>(). For recurring work, derive from PeriodicTenantBackgroundService<TKey> instead.

Methods

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.

protected override Task ExecuteAsync(CancellationToken stoppingToken)

Parameters:

  • stoppingToken CancellationToken: Triggered when StopAsync(CancellationToken) is called.

Returns: Task: A Task that represents the long running operations.

See Worker Services in .NET for implementation guidelines.

ExecuteForTenantAsync(ITenantDescriptor<TKey>, IServiceProvider, CancellationToken)

Performs the work for a single tenant. Called inside that tenant's scope, so scoped services resolved from scopedServices see the tenant.

protected abstract Task ExecuteForTenantAsync(ITenantDescriptor<TKey> tenant, IServiceProvider scopedServices, CancellationToken cancellationToken)

Parameters:

  • tenant ITenantDescriptor<TKey>: The tenant being processed.
  • scopedServices IServiceProvider: A service provider scoped to tenant.
  • cancellationToken CancellationToken: A cancellation token tied to host shutdown.

Returns: Task

RunForAllTenantsAsync(CancellationToken)

Runs TenantBackgroundService<TKey>.ExecuteForTenantAsync for every tenant in the store, one at a time, with per-tenant failure isolation. Exposed so PeriodicTenantBackgroundService<TKey> can invoke it on each tick.

protected Task RunForAllTenantsAsync(CancellationToken cancellationToken)

Parameters:

  • cancellationToken CancellationToken: Stops the run: no tenant starts after it is cancelled.

Returns: Task

On this page