EngUtil.Hosting.CronJob 1.0.0-beta.1

This is a prerelease version of EngUtil.Hosting.CronJob.
dotnet add package EngUtil.Hosting.CronJob --version 1.0.0-beta.1
                    
NuGet\Install-Package EngUtil.Hosting.CronJob -Version 1.0.0-beta.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EngUtil.Hosting.CronJob" Version="1.0.0-beta.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EngUtil.Hosting.CronJob" Version="1.0.0-beta.1" />
                    
Directory.Packages.props
<PackageReference Include="EngUtil.Hosting.CronJob" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EngUtil.Hosting.CronJob --version 1.0.0-beta.1
                    
#r "nuget: EngUtil.Hosting.CronJob, 1.0.0-beta.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package EngUtil.Hosting.CronJob@1.0.0-beta.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EngUtil.Hosting.CronJob&version=1.0.0-beta.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=EngUtil.Hosting.CronJob&version=1.0.0-beta.1&prerelease
                    
Install as a Cake Tool

EngUtil.Hosting.CronJob

NuGet License: MIT .NET

A lightweight cron job hosting library for .NET applications with configuration support, error handling and scheduling integration.


Packages

Package Description
EngUtil.Hosting.CronJob Core library – base class, scheduler, provider
EngUtil.Hosting.CronJob.AspNetCore Optional ASP.NET Core management endpoints

Installation

dotnet add package EngUtil.Hosting.CronJob

For the optional ASP.NET Core management endpoints:

dotnet add reference .\EngUtil.Hosting.CronJob.Extension\EngUtil.Hosting.CronJob.AspNetCore.csproj

Note: the ASP.NET Core extension project currently exists in this repository as a project/package named EngUtil.Hosting.CronJob.AspNetCore. If you publish it to NuGet later, replace the command above with dotnet add package EngUtil.Hosting.CronJob.AspNetCore.


Getting Started

1. Implement a Cron Job

Derive from CronJobBase and decorate the class with [CronJob]:

[CronJob("0 * * * *", disabled: false, runOnStartUp: true, errorThreshold: 5)]
public class MyHourlyCronJob : CronJobBase
{
    public MyHourlyCronJob(IServiceProvider services, ILogger<MyHourlyCronJob> logger)
        : base(services, logger)
    {
        DoWorkAction = async cancellationToken =>
        {
            Logger.LogInformation("Job executed at {Time}", DateTimeOffset.Now);
            await Task.CompletedTask;
        };
    }
}

If you use TaskCompletionSource, ensure it is completed or linked to the provided CancellationToken so the job can shut down cleanly:

DoWorkAction = async cancellationToken =>
{
    var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
    using var registration = cancellationToken.Register(() => tcs.TrySetCanceled(cancellationToken));

    // trigger completion from your async callback/event handler
    // tcs.TrySetResult();

    await tcs.Task;
};

2. Register & Start

// Program.cs
var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddCronJobs();

var app = builder.Build();
app.StartCronJobs();
await app.RunAsync();

AddCronJobs() registers all discovered cron job types as singletons and StartCronJobs() initializes and schedules them.


Cron Expression Format

Standard 5-field cron format is supported:

┌───────────── minute  (0–59)
│ ┌─────────── hour    (0–23)
│ │ ┌───────── day     (1–31)
│ │ │ ┌─────── month   (1–12)
│ │ │ │ ┌───── day of week (0–7, 0 and 7 = Sunday)
│ │ │ │ │
* * * * *

An optional 6-field format (with leading seconds field) and a 7-field format (with trailing year) are also accepted.

Special Characters

Symbol Meaning
* Any value
, List of values (1,5,10)
- Range (1-5)
/ Step (*/5)
L Last day of month / last weekday
W Nearest weekday (15W)
# N-th weekday of month (5#3 = 3rd Friday)

The parser also treats both 0 and 7 as Sunday.

Examples

Expression Schedule
* * * * * Every minute
0 * * * * Every full hour
0 8 * * 1-5 Every weekday at 08:00
0 0 1 * * First day of every month at midnight
*/15 * * * * Every 15 minutes

Configuration

Inline (Attribute)

All parameters are defined directly on [CronJob]:

[CronJob(
    cronExpression: "0 8 * * 1-5",
    disabled: false,
    runOnStartUp: false,
    visible: true,
    errorThreshold: 5,
    errorThresholdResetTime: 60)]
public class MyJob : CronJobBase { … }
Parameter Type Default Description
cronExpression string Cron schedule
disabled bool false Disable job without removing it
runOnStartUp bool false Run once immediately on host start
visible bool true Show in monitoring/management views
errorThreshold int 5 Max consecutive errors before job stops
errorThresholdResetTime int -1 Minutes until error count resets (-1 = never)

External (appsettings.json)

Supply a custom config type to load settings from IConfiguration:

[CronJob(typeof(MyJobConfig))]
public class MyExternalCronJob : CronJobBase { … }
// appsettings.json
{
  "CronJobs": {
    "MyExternalCronJob": {
      "CronExpression": "0 6 * * *",
      "Disabled": false,
      "RunOnStartup": true,
      "ErrorThreshold": 3,
      "ErrorThresholdResetTime": 30
    }
  }
}

Configuration lookup is performed under:

  • CronJobs:{FullTypeName}
  • fallback: CronJobs:{TypeName}

Error Handling

Subscribe to the ErrorOccurred event on any job instance or on ICronJobProvider:

var provider = app.Services.GetRequiredService<ICronJobProvider>();
provider.ErrorOccurred += (sender, args) =>
{
    Console.Error.WriteLine($"[{args.JobType}] Error: {args.Exception.Message}");
};

When the number of consecutive errors reaches ErrorThreshold, the job stops scheduling itself automatically. If ErrorThresholdResetTime is set to a value greater than 0, the error counter resets after the specified number of minutes and the job can be restarted automatically.

Cancellation via the provided CancellationToken is treated as a normal shutdown path and not as an execution error.


ASP.NET Core Management Endpoints

The optional EngUtil.Hosting.CronJob.AspNetCore package adds a REST API for runtime management.

Setup

Reference the ASP.NET Core extension project/package and then register the endpoints in your WebApplication:

// Program.cs
app.AddCronJobEnpoints();

With authorization:

app.AddCronJobEnpoints(o =>
{
    o.RequireAuthorization = true;
    o.Policy = "CronJobAdmin"; // optional named policy
});

Endpoints

All endpoints are registered under /api/CronJob:

Method Route Description
GET /api/CronJob List all visible cron jobs with their status
GET /api/CronJob/{jobType}/Start Start a job
GET /api/CronJob/{jobType}/Stop Stop a job
GET /api/CronJob/{jobType}/Run Execute a job once immediately
GET /api/CronJob/{jobType}/Cancel Cancel the currently running execution
GET /api/CronJob/{jobType}/Reschedule Stop and restart a job
GET /api/CronJob/{jobType}/Exists Check whether a job is registered

The GET /api/CronJob endpoint returns only jobs where Visible == true.


Target Frameworks

Framework Version
Core library .NET 8, 9, 10
ASP.NET Core extension .NET 9

License

This project is licensed under the MIT License.
© 2019–2026 Oliver Engels

Product 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on EngUtil.Hosting.CronJob:

Package Downloads
EngUtil.Hosting.CronJob.AspNetCore

ASP.NET Core extension for EngUtil.Hosting.CronJob – exposes cron job management endpoints via Minimal API.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-beta.1 56 5/11/2026