MinimalWorker 1.0.16
dotnet add package MinimalWorker --version 1.0.16
NuGet\Install-Package MinimalWorker -Version 1.0.16
<PackageReference Include="MinimalWorker" Version="1.0.16" />
<PackageVersion Include="MinimalWorker" Version="1.0.16" />
<PackageReference Include="MinimalWorker" />
paket add MinimalWorker --version 1.0.16
#r "nuget: MinimalWorker, 1.0.16"
#:package MinimalWorker@1.0.16
#addin nuget:?package=MinimalWorker&version=1.0.16
#tool nuget:?package=MinimalWorker&version=1.0.16
MinimalWorker
MinimalWorker is a lightweight .NET library that simplifies background worker registration in ASP.NET Core and .NET applications using the IHost
interface. It offers two simple extension methods to map background tasks that run continuously or periodically, with support for dependency injection and cancellation tokens.
โจ Features
- ๐ 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
๐ฆ Installation
Install from NuGet:
dotnet add package MinimalWorker
Or via the NuGet Package Manager:
Install-Package MinimalWorker
๐ Usage
Continuous Background Worker
app.MapBackgroundWorker(async (MyService service, CancellationToken token) =>
{
while (!token.IsCancellationRequested)
{
await service.DoWorkAsync();
await Task.Delay(1000, token);
}
});
Periodic Background Worker
app.MapPeriodicBackgroundWorker(TimeSpan.FromMinutes(5), async (MyService service, CancellationToken token) =>
{
await service.CleanupAsync();
});
Command run on notice (Cron) Background Worker
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.
๐ง How It Works
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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. 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. |
-
net8.0
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.4)
- NCrontab (>= 3.3.3)
-
net9.0
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.4)
- 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.