MiniCron.Core
1.3.0
dotnet add package MiniCron.Core --version 1.3.0
NuGet\Install-Package MiniCron.Core -Version 1.3.0
<PackageReference Include="MiniCron.Core" Version="1.3.0" />
<PackageVersion Include="MiniCron.Core" Version="1.3.0" />
<PackageReference Include="MiniCron.Core" />
paket add MiniCron.Core --version 1.3.0
#r "nuget: MiniCron.Core, 1.3.0"
#:package MiniCron.Core@1.3.0
#addin nuget:?package=MiniCron.Core&version=1.3.0
#tool nuget:?package=MiniCron.Core&version=1.3.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
Documentation
Comprehensive guides and examples are available to help you integrate MiniCron.Core into your projects:
Examples
- 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.
Guides
- 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.
Quick Start
Console Application:
dotnet new console -n MyApp
cd MyApp
dotnet add package MiniCron.Core
dotnet add package Microsoft.Extensions.Hosting
See the Console Application Example for complete code.
Web Application:
dotnet new web -n MyWebApp
cd MyWebApp
dotnet add package MiniCron.Core
See the Web Application Example for complete code.
Contributing
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ by jeanlrnt
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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 is compatible. 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. |
-
net10.0
- Microsoft.Extensions.Hosting (>= 8.0.1)
-
net6.0
- Microsoft.Extensions.Hosting (>= 8.0.1)
-
net7.0
- Microsoft.Extensions.Hosting (>= 8.0.1)
-
net8.0
- Microsoft.Extensions.Hosting (>= 8.0.1)
-
net9.0
- Microsoft.Extensions.Hosting (>= 8.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.