Atya.Hosting.BackgroundServices 1.0.0

Prefix Reserved
dotnet add package Atya.Hosting.BackgroundServices --version 1.0.0
                    
NuGet\Install-Package Atya.Hosting.BackgroundServices -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Atya.Hosting.BackgroundServices" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Atya.Hosting.BackgroundServices" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Atya.Hosting.BackgroundServices" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Atya.Hosting.BackgroundServices --version 1.0.0
                    
#r "nuget: Atya.Hosting.BackgroundServices, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Atya.Hosting.BackgroundServices@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Atya.Hosting.BackgroundServices&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Atya.Hosting.BackgroundServices&version=1.0.0
                    
Install as a Cake Tool

Atya.Hosting.BackgroundServices

Small hosted-service execution helpers for production .NET workers.

Overview

Atya.Hosting.BackgroundServices provides narrow BackgroundService base classes for repeatable worker loops. It keeps cancellation, retry delays, consecutive-failure limits, per-iteration dependency injection scopes, and lifecycle logging consistent without introducing a worker framework or scheduler.

Installation

dotnet add package Atya.Hosting.BackgroundServices

Quick Start

using Atya.Hosting.BackgroundServices;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        services.AddAtyaBackgroundService<InboxWorker>(options =>
        {
            options.Interval = TimeSpan.FromSeconds(30);
            options.FailureDelay = TimeSpan.FromSeconds(5);
            options.MaxConsecutiveFailures = 3;
        });
    })
    .Build();

await host.RunAsync();

internal sealed class InboxWorker(ILogger<InboxWorker> logger)
    : PeriodicBackgroundService(logger)
{
    protected override Task ExecuteIterationAsync(CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}

Feature Tour

PeriodicBackgroundService

Derive from PeriodicBackgroundService and implement one unit of work in ExecuteIterationAsync. The base class calls it repeatedly until the host cancellation token is canceled.

ScopedPeriodicBackgroundService

Derive from ScopedPeriodicBackgroundService when the worker needs scoped services. Each iteration receives an IServiceProvider from a fresh scope, and that scope is disposed after the iteration completes. This avoids capturing scoped dependencies in the hosted-service singleton.

internal sealed class ScopedInboxWorker(
    ILogger<ScopedInboxWorker> logger,
    IServiceScopeFactory scopeFactory)
    : ScopedPeriodicBackgroundService(logger, scopeFactory)
{
    protected override Task ExecuteIterationAsync(IServiceProvider scopedServices, CancellationToken cancellationToken)
    {
        var handler = scopedServices.GetRequiredService<InboxHandler>();
        return handler.ProcessAsync(cancellationToken);
    }
}

PeriodicBackgroundServiceOptions

Configure Interval, FailureDelay, and MaxConsecutiveFailures. A successful iteration resets the consecutive-failure count; reaching the failure limit lets the original exception escape to the host.

Registration Helpers

Use AddAtyaBackgroundService<TService> for PeriodicBackgroundService implementations. Use AddAtyaScopedBackgroundService<TService> for ScopedPeriodicBackgroundService implementations. Both helpers can configure PeriodicBackgroundServiceOptions.

Error Codes

This package does not define error codes. Programmer errors are reported through argument exceptions from guard checks, and worker failures remain the original exceptions thrown by the worker implementation.

Why These Dependencies

  • Atya.Foundation.Guards keeps public argument validation consistent across Atya packages.
  • Atya.Diagnostics.Logging provides shared structured logging helpers.
  • Microsoft.Extensions.Hosting.Abstractions, Microsoft.Extensions.Logging.Abstractions, Microsoft.Extensions.DependencyInjection.Abstractions, and Microsoft.Extensions.Options are the platform abstractions this package extends.
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 105 7/15/2026