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.
- 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.
Install the package via NuGet:
dotnet add package MiniCron.CoreIn 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();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);
});
});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
Comprehensive guides and examples are available to help you integrate MiniCron.Core into your projects:
- Console Application Example - Learn how to integrate MiniCron.Core into a minimal .NET Console application with step-by-step instructions and working examples.
- Web Application Example - Complete guide for ASP.NET Core applications including Minimal API, MVC, Web API, and Blazor Server examples with Entity Framework Core integration.
- Configuration Guide - Comprehensive information about configuring MiniCron.Core, including configuration sources, environment-specific settings, and a complete cron expression reference.
- Advanced Scenarios Guide - Advanced usage patterns including error handling strategies, working with third-party services, distributed systems considerations, and performance optimization.
Console Application:
dotnet new console -n MyApp
cd MyApp
dotnet add package MiniCron.Core
dotnet add package Microsoft.Extensions.HostingSee the Console Application Example for complete code.
Web Application:
dotnet new web -n MyWebApp
cd MyWebApp
dotnet add package MiniCron.CoreSee the Web Application Example for complete code.
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ by jeanlrnt