MiniCron.Core
1.0.0
See the version list below for details.
dotnet add package MiniCron.Core --version 1.0.0
NuGet\Install-Package MiniCron.Core -Version 1.0.0
<PackageReference Include="MiniCron.Core" Version="1.0.0" />
<PackageVersion Include="MiniCron.Core" Version="1.0.0" />
<PackageReference Include="MiniCron.Core" />
paket add MiniCron.Core --version 1.0.0
#r "nuget: MiniCron.Core, 1.0.0"
#:package MiniCron.Core@1.0.0
#addin nuget:?package=MiniCron.Core&version=1.0.0
#tool nuget:?package=MiniCron.Core&version=1.0.0
MiniCron.Core
MiniCron.Core is a lightweight and simple library for scheduling background tasks in .NET applications using Cron expressions. It integrates seamlessly with the Microsoft.Extensions.DependencyInjection framework.
Features
- Simple & Lightweight: Minimal setup required.
- Dependency Injection: Access your services directly within your scheduled jobs.
- Async Support: Fully supports asynchronous tasks.
- Cancellation Support: Handles cancellation tokens gracefully.
Installation
Install the package via NuGet:
dotnet add package MiniCron.Core
Usage
1. Register MiniCron
In your Program.cs (or Startup.cs), register MiniCron services and define your jobs:
using MiniCron.Core.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Register MiniCron
builder.Services.AddMiniCron(options =>
{
// Run every minute
options.AddJob("* * * * *", async (serviceProvider, cancellationToken) =>
{
Console.WriteLine($"Job executed at {DateTime.Now}");
await Task.CompletedTask;
});
// Run every 5 minutes
options.AddJob("*/5 * * * *", async (serviceProvider, cancellationToken) =>
{
// Resolve services from the DI container
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
logger.LogInformation("5-minute job executed.");
await Task.Delay(100, cancellationToken);
});
});
var app = builder.Build();
app.Run();
2. Using Scoped Services
Since the background service runs as a Singleton, you should create a scope to resolve Scoped services (like EF Core DbContexts):
builder.Services.AddMiniCron(options =>
{
options.AddJob("0 12 * * *", async (serviceProvider, cancellationToken) =>
{
using var scope = serviceProvider.CreateScope();
var myScopedService = scope.ServiceProvider.GetRequiredService<IMyScopedService>();
await myScopedService.DoWorkAsync(cancellationToken);
});
});
Cron Expressions
MiniCron supports standard cron expressions with 5 fields:
* * * * *
| | | | |
| | | | +----- Day of Week (0 - 6) (Sunday=0)
| | | +------- Month (1 - 12)
| | +--------- Day of Month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
Examples:
* * * * *: Every minute*/5 * * * *: Every 5 minutes0 0 * * *: Every day at midnight0 12 * * 1: Every Monday at 12:00 PM
Made with ❤️ by jeanlrnt
| 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 was computed. 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 (>= 10.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.