![]() |
VOOZH | about |
dotnet add package DotNetBrightener.Core.BackgroundTasks --version 2026.0.2
NuGet\Install-Package DotNetBrightener.Core.BackgroundTasks -Version 2026.0.2
<PackageReference Include="DotNetBrightener.Core.BackgroundTasks" Version="2026.0.2" />
<PackageVersion Include="DotNetBrightener.Core.BackgroundTasks" Version="2026.0.2" />Directory.Packages.props
<PackageReference Include="DotNetBrightener.Core.BackgroundTasks" />Project file
paket add DotNetBrightener.Core.BackgroundTasks --version 2026.0.2
#r "nuget: DotNetBrightener.Core.BackgroundTasks, 2026.0.2"
#:package DotNetBrightener.Core.BackgroundTasks@2026.0.2
#addin nuget:?package=DotNetBrightener.Core.BackgroundTasks&version=2026.0.2Install as a Cake Addin
#tool nuget:?package=DotNetBrightener.Core.BackgroundTasks&version=2026.0.2Install as a Cake Tool
Copyright © 2017 - 2026 Vampire Coder (formerly DotnetBrightener)
The DotNetBrightener Background Tasks module provides a comprehensive, cron-based task scheduling system for .NET applications. It enables developers to schedule and execute background tasks with precise timing control, overlap prevention, and robust error handling.
IBackgroundTask) and method-based tasksThe main interface for scheduling and managing background tasks. Provides methods to:
Interface that background task classes must implement:
public interface IBackgroundTask
{
Task Execute();
}
Extended interface for tasks that support cancellation:
public interface ICancellableTask : IBackgroundTask
{
CancellationToken CancellationToken { get; set; }
}
Fluent interface for configuring task schedules with methods like:
EverySecond(), EveryMinute(), Hourly(), Daily()Cron(string expression)PreventOverlapping()When(Func<Task<bool>> predicate)AtTimeZone(TimeZoneInfo timeZoneInfo)Add the background tasks services to your application:
var builder = WebApplication.CreateBuilder(args);
// Enable background task services
builder.Services.EnableBackgroundTaskServices(builder.Configuration);
var app = builder.Build();
public class EmailCleanupTask : IBackgroundTask
{
private readonly IEmailService _emailService;
private readonly ILogger<EmailCleanupTask> _logger;
public EmailCleanupTask(IEmailService emailService, ILogger<EmailCleanupTask> logger)
{
_emailService = emailService;
_logger = logger;
}
public async Task Execute()
{
_logger.LogInformation("Starting email cleanup task");
await _emailService.DeleteOldEmails();
_logger.LogInformation("Email cleanup task completed");
}
}
public class DataProcessingTask : ICancellableTask
{
public CancellationToken CancellationToken { get; set; }
public async Task Execute()
{
while (!CancellationToken.IsCancellationRequested)
{
// Process data
await ProcessBatch();
await Task.Delay(1000, CancellationToken);
}
}
}
// Register background tasks
builder.Services.AddBackgroundTask<EmailCleanupTask>();
builder.Services.AddBackgroundTask<DataProcessingTask>();
var scheduler = app.Services.GetService<IScheduler>();
// Schedule with predefined intervals
scheduler.ScheduleTask<EmailCleanupTask>()
.Daily()
.PreventOverlapping();
// Schedule with custom intervals
scheduler.ScheduleTask<DataProcessingTask>()
.EverySeconds(30)
.PreventOverlapping();
// Schedule with cron expressions
scheduler.ScheduleTask<EmailCleanupTask>()
.Cron("0 2 * * *") // Daily at 2 AM
.AtTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
// One-time execution
scheduler.ScheduleTask<DataProcessingTask>()
.Once();
You can schedule methods directly without implementing IBackgroundTask:
public class UtilityService
{
public async Task CleanupTempFiles()
{
// Cleanup logic
}
public void GenerateReports()
{
// Report generation logic
}
}
// Schedule methods
var methodInfo = typeof(UtilityService).GetMethod(nameof(UtilityService.CleanupTempFiles));
scheduler.ScheduleTask(methodInfo)
.Hourly()
.PreventOverlapping();
Execute tasks only when certain conditions are met:
scheduler.ScheduleTask<BackupTask>()
.Daily()
.When(async () => await IsMaintenanceWindowOpen())
.PreventOverlapping();
scheduler.ScheduleTask<ReportTask>()
.DailyAt(9, 0) // 9:00 AM
.AtTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));
Configure the scheduler interval in appsettings.json:
{
"BackgroundTaskOptions": {
"Interval": "00:00:30"
}
}
For persistent task definitions and execution history:
builder.Services.AddBackgroundTaskStorage(options =>
{
options.UseSqlServer(connectionString);
});
This creates a BackgroundTaskDefinition table to store:
The background tasks system publishes events through the EventPubSub system:
ScheduledEventStarted: Published when a task begins executionScheduledEventEnded: Published when a task completes successfullyScheduledEventFailed: Published when a task throws an exceptionpublic class TaskMonitoringHandler : IEventHandler<ScheduledEventFailed>
{
public async Task<bool> HandleEvent(ScheduledEventFailed eventMessage)
{
// Log error, send notifications, etc.
return true;
}
}
EverySecond() - Every secondEverySeconds(int seconds) - Every N secondsEveryMinute() - Every minuteEveryFiveMinutes() - Every 5 minutesEveryTenMinutes() - Every 10 minutesEveryFifteenMinutes() - Every 15 minutesEveryThirtyMinutes() - Every 30 minutesHourly() - Every hourHourlyAt(int minute) - Every hour at specified minuteDaily() - Every day at midnightDailyAt(int hour, int minute) - Every day at specified timeWeekly() - Every weekMonthly() - Every monthMonday(), Tuesday(), Wednesday(), Thursday(), Friday(), Saturday(), Sunday()Weekday() - Monday through FridayWeekend() - Saturday and SundaySupport for standard 5 or 6-part cron expressions:
minute hour day month weekdaysecond minute hour day month weekdayExamples:
"0 2 * * *" - Daily at 2:00 AM"*/15 * * * *" - Every 15 minutes"0 0 * * 0" - Every Sunday at midnight"30 14 1 * *" - 2:30 PM on the 1st of every monthThe system provides comprehensive logging at various levels:
ScheduledEventFailed events are published for monitoringICancellableTask for long-running tasksPreventOverlapping() for tasks that shouldn't run concurrentlyEnableBackgroundTaskServices() is calledAddBackgroundTask<T>()Enable detailed logging to troubleshoot issues:
builder.Services.AddLogging(logging =>
{
logging.SetMinimumLevel(LogLevel.Debug);
logging.AddConsole();
});
When using database storage, the system automatically handles database migrations through the MigrateBackgroundTaskDbContextHostedService.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 3 NuGet packages that depend on DotNetBrightener.Core.BackgroundTasks:
| Package | Downloads |
|---|---|
|
DotNetBrightener.WebApp.CommonShared
Package Description |
|
|
DotNetBrightener.Core.BackgroundTasks.DependencyInjection
Package Description |
|
|
DotNetBrightener.Core.BackgroundTasks.Data
Package Description |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2026.0.3-preview-777 | 202 | 5/20/2026 |
| 2026.0.3-preview-773 | 181 | 4/24/2026 |
| 2026.0.3-preview-772 | 200 | 4/3/2026 |
| 2026.0.3-preview-770 | 163 | 4/2/2026 |
| 2026.0.3-preview-769 | 159 | 4/2/2026 |
| 2026.0.2 | 184 | 4/2/2026 |
| 2026.0.2-preview-v2026-0-1-755 | 187 | 3/27/2026 |
| 2026.0.2-preview-759 | 177 | 4/1/2026 |
| 2026.0.2-preview-758 | 166 | 3/29/2026 |
| 2026.0.2-preview-757 | 168 | 3/29/2026 |
| 2026.0.2-preview-756 | 170 | 3/27/2026 |
| 2026.0.2-preview-754 | 158 | 3/27/2026 |
| 2026.0.1 | 178 | 3/27/2026 |
| 2026.0.1-preview-752 | 157 | 3/26/2026 |
| 2026.0.1-preview-750 | 156 | 3/26/2026 |
| 2026.0.1-preview-749 | 157 | 3/25/2026 |
| 2025.0.11-preview-776 | 153 | 5/20/2026 |
| 2025.0.11-preview-771 | 154 | 4/2/2026 |
| 2025.0.11-preview-768 | 149 | 4/2/2026 |
| 2025.0.11-preview-762 | 148 | 4/2/2026 |