Dalog.Foundation.BackgroundServices
0.9.3
There is a newer prerelease version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Dalog.Foundation.BackgroundServices --version 0.9.3
NuGet\Install-Package Dalog.Foundation.BackgroundServices -Version 0.9.3
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="Dalog.Foundation.BackgroundServices" Version="0.9.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dalog.Foundation.BackgroundServices" Version="0.9.3" />
<PackageReference Include="Dalog.Foundation.BackgroundServices" />
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 Dalog.Foundation.BackgroundServices --version 0.9.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Dalog.Foundation.BackgroundServices, 0.9.3"
#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 Dalog.Foundation.BackgroundServices@0.9.3
#: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=Dalog.Foundation.BackgroundServices&version=0.9.3
#tool nuget:?package=Dalog.Foundation.BackgroundServices&version=0.9.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Background Services
A .NET library providing robust background service implementations for channel-based message processing, cron-scheduled task execution, Azure Service Bus message processing, and Azure Event Hub message processing.
Features
Channel Background Services
- Queue-based processing: Process items from an unbounded channel
- Retry logic: Configurable retry attempts with delays
- Error handling: Custom error callbacks for failed items
- Graceful shutdown: Option to drain queue on service shutdown
- Timeout management: Configurable timeouts for item processing
Cron Background Services
- Cron scheduling: Support for cron expressions with optional seconds
- Flexible execution: Choice between waiting for completion or fire-and-forget
- Timeout support: Configurable timeouts for scheduled tasks
Azure Service Bus Background Services
- Message processing: Process messages from Azure Service Bus queues
- JSON serialization: Automatic JSON deserialization of message bodies
- Application properties: Access to message application properties
- Prefetch support: Configurable prefetch count for performance optimization
- Auto-complete: Automatic message completion on successful processing
Azure Event Hub Background Services
- Event processing: Process events from Azure Event Hubs in real-time
- JSON serialization: Automatic JSON deserialization of event bodies
- Checkpoint management: Automatic checkpoint management using Azure Storage
- Load balancing: Automatic load balancing across multiple instances
- Consumer group support: Configurable consumer group for event processing
- Performance optimization: Configurable prefetch count and wait times
Installation
dotnet add package Dalog.Foundation.BackgroundServices
Usage
Channel Background Service
// Define your handler
public class EmailHandler : IChannelHandler<EmailMessage>
{
public async Task Handle(EmailMessage item, CancellationToken cancellationToken)
{
// Process the email message
await SendEmailAsync(item);
}
}
// Register in DI container
services.AddChannelBackgroundService<EmailMessage, EmailHandler>(options =>
{
options.TimeoutInMinutes = 5;
options.RetryAttempts = 3;
options.RetryDelay = TimeSpan.FromSeconds(30);
options.DrainQueueOnShutdown = true;
});
// Enqueue items for processing
var writer = serviceProvider.GetRequiredService<IChannelWriter<EmailMessage>>();
await writer.Enqueue(new EmailMessage("test@example.com", "Hello World"));
Cron Background Service
// Define your handler
public class MaintenanceHandler : ICronHandler
{
public async Task Handle(CancellationToken cancellationToken)
{
// Perform maintenance tasks
await RunMaintenanceAsync();
}
}
// Register in DI container
services.AddCronBackgroundService<MaintenanceHandler>(options =>
{
options.CronExpression = "0 2 * * *"; // Run daily at 2 AM
options.TimeoutInMinutes = 30;
options.WaitForRequestCompletion = true;
});
Azure Service Bus Background Service
// Define your message type
public class OrderMessage
{
public string OrderId { get; set; }
public decimal Amount { get; set; }
}
// Define your handler
public class OrderHandler : IAzureServiceBusHandler<OrderMessage>
{
public async Task Handle(OrderMessage message, IReadOnlyDictionary<string, object> applicationProperties,
CancellationToken cancellationToken)
{
// Process the order message
await ProcessOrderAsync(message);
}
}
// Register in DI container
services.AddAzureServiceBusBackgroundService<OrderMessage, OrderHandler>(options =>
{
options.ConnectionString = "Endpoint=sb://myservicebus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=...";
options.QueueName = "orders";
options.PrefetchCount = 10;
});
// Or use the simplified overload
services.AddAzureServiceBusBackgroundService<OrderMessage, OrderHandler>(
connectionString: "Endpoint=sb://myservicebus.servicebus.windows.net/;...",
queueName: "orders");
Azure Event Hub Background Service
// Define your message type
public class TelemetryMessage
{
public string DeviceId { get; set; }
public double Temperature { get; set; }
public DateTime Timestamp { get; set; }
}
// Define your handler
public class TelemetryHandler : IAzureEventHubHandler<TelemetryMessage>
{
public async Task Handle(TelemetryMessage message, CancellationToken cancellationToken)
{
// Process the telemetry message
await ProcessTelemetryAsync(message);
}
}
// Register in DI container
services.AddAzureEventHubBackgroundService<TelemetryMessage, TelemetryHandler>(options =>
{
options.StorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=mystorageaccount;AccountKey=...";
options.StorageContainerName = "checkpoints";
options.EventHubConnectionString = "Endpoint=sb://myeventhub.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=...;EntityPath=telemetry";
options.ConsumerGroupName = "telemetry-processors";
options.PrefetchCount = 100;
});
Testing
The library includes comprehensive unit tests covering:
Channel Services
- Injection Tests: Service registration and dependency injection
- Service Tests: Core channel functionality (enqueue, dequeue, completion)
- Options Tests: Configuration validation and default values
Cron Services
- Injection Tests: Service registration with various cron expressions
- Options Tests: Configuration validation and cron expression parsing
- Validation Tests: Invalid cron expression handling
Azure Service Bus Services
- Injection Tests: Service registration and dependency injection
- Options Tests: Configuration validation and connection string handling
Azure Event Hub Services
- Injection Tests: Service registration and dependency injection
- Options Tests: Configuration validation and connection string handling
Test Coverage
- ✅ 110 unit tests covering all public APIs
- ✅ Dependency injection validation
- ✅ Configuration options testing
- ✅ Error handling scenarios
- ✅ Cancellation token support
- ✅ Invalid input validation
Run tests:
dotnet test
Configuration Options
ChannelBackgroundServiceOptions
TimeoutInMinutes: Maximum processing time per item (default: 32)RetryAttempts: Number of retry attempts on failure (default: 3)RetryDelay: Delay between retries (default: 30 seconds)DrainQueueOnShutdown: Whether to process remaining items on shutdown (default: true)OnError: Custom error handling callback
CronBackgroundServiceOptions
CronExpression: Cron schedule expressionTimeoutInMinutes: Maximum execution time (default: 33)WaitForRequestCompletion: Wait for previous execution to complete (default: true)IncludingSeconds: Whether cron expression includes seconds (default: false)
AzureServiceBusBackgroundServiceOptions
ConnectionString: Azure Service Bus connection stringQueueName: Name of the Azure Service Bus queuePrefetchCount: Number of messages to prefetch (default: 0)
AzureEventHubBackgroundServiceOptions
StorageConnectionString: Azure Storage account connection string for checkpointingStorageContainerName: Name of the Azure Storage container for checkpointsEventHubConnectionString: Azure Event Hub connection stringConsumerGroupName: Consumer group name for event processing (default: "$Default")PrefetchCount: Number of events to prefetch for performance (default: 300)MaximumWaitTime: Maximum time to wait for events (default: 10 seconds)LoadBalancingUpdateInterval: Interval for load balancing updates (default: 30 seconds)
License
Copyright (C) DALOG Diagnosesysteme GmbH - All Rights Reserved
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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.
-
net9.0
- Azure.Messaging.EventHubs.Processor (>= 5.12.2)
- Azure.Messaging.ServiceBus (>= 7.20.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.8)
- NCrontab (>= 3.3.3)
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 |
|---|---|---|
| 0.9.4-PullRequest18.3 | 150 | 8/12/2025 |
| 0.9.4-PullRequest18.2 | 140 | 8/12/2025 |
| 0.9.3 | 293 | 8/8/2025 |
| 0.9.3-PullRequest16.16 | 169 | 8/8/2025 |
| 0.9.3-PullRequest16.15 | 168 | 8/8/2025 |
| 0.9.3-PullRequest16.14 | 172 | 8/8/2025 |
| 0.9.3-PullRequest14.12 | 173 | 8/8/2025 |
| 0.9.3-PullRequest10.4 | 190 | 8/8/2025 |
| 0.9.2 | 271 | 8/6/2025 |
| 0.9.1 | 263 | 8/6/2025 |
| 0.9.0 | 271 | 8/6/2025 |