This is a demo app that executes CRON jobs using IHostedService
and BackgroundService
of ASP.NET Core.
Documentation of ASP.NET Core background services: https://learn.microsoft.com/en-us/dotnet/architecture/microservices/multi-container-microservice-net-applications/background-tasks-with-ihostedservice
- Job classes are regenerated each time they are executed
- If the job class implements
IDisposable
,Dispose()
will be called after the execution is finished - Dependency injection can be performed in the job class constructor
- You can specify any parameter to the job class
Customized based on dotnet-labs/ServiceWorkerCronJob.
public class CronJob : IJobWorker
{
public Task DoWorkAsync(CancellationToken cancellationToken)
{
// Do work here
}
}
builder.Services.AddCronJob(
options =>
{
options.CronExpression = "*/1 * * * *";
},
provider => ActivatorUtilities.CreateInstance<CronJob>(provider));
The MIT License(MIT)