A lightweight background service library for ASP.NET Core to manage SMS, Email, and custom background tasks with simple configuration.
$ dotnet add package Coject.BackgroundServicesCopyright (c) 2024 Akwad Arabia
A lightweight and easy-to-use background service library for ASP.NET Core applications. Manage SMS, Email, and custom background tasks with simple configuration.
✅ Easy Integration - One line of code to enable background services
✅ Configurable - Enable/disable services via appsettings.json
✅ Extensible - Add custom background tasks easily
✅ Logging Support - Built-in logging for all operations
✅ Scoped Services - Proper dependency injection with scoped lifetimes
dotnet add package Coject.BackgroundServices
Or via NuGet Package Manager:
Install-Package Coject.BackgroundServices
dotnet add package Coject.BackgroundServices
{
"BackgroundService": {
"EnabledServices": ["ServiceSMS", "ServiceMail"],
"IntervalSeconds": 10
}
}
using Coject.BackgroundServices.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Register your core dependencies
builder.Services.AddScoped<CoreModuleDB>();
builder.Services.AddScoped<ApiContext>();
// Add background services with one line
builder.Services.AddCojectBackgroundServices(builder.Configuration);
var app = builder.Build();
app.Run();
Your background services will start automatically and run based on the configured interval.
| Property | Type | Description | Default |
|---|---|---|---|
EnabledServices | string[] | Array of service names to enable | [] |
IntervalSeconds | int | Interval between executions in seconds | 60 |
ServiceSMS - SMS message processingServiceMail - Email message processingRun every 10 seconds:
{
"BackgroundService": {
"EnabledServices": ["ServiceSMS", "ServiceMail"],
"IntervalSeconds": 10
}
}
Enable only SMS:
{
"BackgroundService": {
"EnabledServices": ["ServiceSMS"],
"IntervalSeconds": 30
}
}
Disable all services:
{
"BackgroundService": {
"EnabledServices": [],
"IntervalSeconds": 60
}
}
using Coject.BackgroundServices.Interfaces;
public class CustomBackgroundTask : IBackgroundTask
{
private readonly ILogger<CustomBackgroundTask> _logger;
public string ServiceName => "ServiceCustom";
public CustomBackgroundTask(ILogger<CustomBackgroundTask> logger)
{
_logger = logger;
}
public async Task ExecuteAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Custom task executing...");
// Your logic here
}
}
builder.Services.AddCojectBackgroundServices(builder.Configuration);
builder.Services.AddBackgroundTask<CustomBackgroundTask>();
{
"BackgroundService": {
"EnabledServices": ["ServiceSMS", "ServiceMail", "ServiceCustom"],
"IntervalSeconds": 10
}
}
Your main project must register these dependencies before calling AddCojectBackgroundServices():
builder.Services.AddScoped<CoreModuleDB>();
builder.Services.AddScoped<ApiContext>();
The package uses ILogger for all logging. Configure your logging in appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Coject.BackgroundServices": "Debug"
}
}
}
MIT License - see LICENSE.txt for details
For issues, questions, or contributions, please visit: