MinimalWorker is a lightweight .NET library that simplifies background worker registration in ASP.NET Core and .NET applications using the IHost
interface. It offers three simple extension methods to map background tasks that run continuously or periodically, with support for dependency injection and cancellation tokens.
- 🚀 Register background workers with a single method call
- ⏱ Support for periodic background tasks
- 🔄 Built-in support for
CancellationToken
- 🧪 Works seamlessly with dependency injection (
IServiceProvider
) - 🧼 Minimal and clean API
Install from NuGet:
dotnet add package MinimalWorker
Or via the NuGet Package Manager:
Install-Package MinimalWorker
app.MapBackgroundWorker(async (MyService service, CancellationToken token) =>
{
while (!token.IsCancellationRequested)
{
await service.DoWorkAsync();
await Task.Delay(1000, token);
}
});
app.MapPeriodicBackgroundWorker(TimeSpan.FromMinutes(5), async (MyService service, CancellationToken token) =>
{
await service.CleanupAsync();
});
app.MapCronBackgroundWorker("0 0 * * *", async (CancellationToken ct, MyService service) =>
{
await service.SendDailyProgressReport();
});
All methods automatically resolve services from the DI container and inject the CancellationToken
if it's a parameter.
MapBackgroundWorker
runs a background task once the application starts, and continues until shutdown.MapPeriodicBackgroundWorker
runs your task repeatedly at a fixed interval using PeriodicTimer.MapCronBackgroundWorker
runs your task repeatedly based on a CRON expression (UTC time), using NCrontab for timing.- Services and parameters are resolved per execution using
CreateScope()
to support scoped dependencies.