This project contains the code for the NuGet Package to get scheduled tasks in an asp.net core application.
INFO: For detailed usage information please have a look in the
KK.DotNet.BackgroundTasks.Scheduled.Web.Sample
project.
You can add multiple tasks in your projekt. The task has to implement the IScheduledTask
interface.
The options for the task are provided by the DI system and you can request the options with IScheduledTaskOptions<SampleTask>
as shown below.
The ExecuteAsync
Method will be executed when the task is triggert.
public class SampleTask : IScheduledTask
{
public SampleTask(
IScheduledTaskOptions<SampleTask> options
)
{
this.Options = options;
}
public IScheduledTaskOptions<IScheduledTask> Options { get; }
public async Task ExecuteAsync(CancellationToken stoppingToken)
{
// Stuff to do...
}
}
INFO You shoud use the provided
CancellationToken
in your code to support cancelling of your task.
TIPP If you want to use Scoped Services inside have a look here: https://stackoverflow.com/a/58989132/9277073
You have to register the task in your public void ConfigureServices
of the Startup.cs
. You can use the provided AddScheduledTask
Method:
services.AddScheduledTask<SampleTask>();
The Task will be added as a Singleton
with this method.
If you does not want this you can also register it by yourself:
services.AddScoped<IScheduledTask, SampleTask>();
The options are provided as an IScheduledTaskOptions
of type YourSampleTask
through the DI system
Property | Description | Default |
---|---|---|
Schedule |
A CronTab sting which describes the execution schedule | nothing, must be set by the user. For the format have a look here |
CronFormat |
A Cronos.CronFormat value, this must only be set when you ant to use seconds in your schedule |
Cronos.CronFormat.Standard , more information here |
In your public void ConfigureServices
of the Startup.cs
you can add options like so:
services.AddSingleton<IScheduledTaskOptions<SampleTask>>(
new ScheduledTaskOptions<SampleTask>
{
Schedule = "*/10 * * * * *",
CronFormat = Cronos.CronFormat.IncludeSeconds
}
);
After the registration of all your tasks there is only one thing left to do.
Register the SchedulerHostedService
like so:
services.AddHostedService<SchedulerHostedService>();
Feed | Name | Status |
---|---|---|
NuGet.org | KK.DotNet.BackgroundTasks.Scheduled | |
Azure DevOps | KK.DotNet.BackgroundTasks.Scheduled |
You can add the package for example with the following dotnet
command:
dotnet add package KK.DotNet.BackgroundTasks.Scheduled
Pre-releases of this Package are pushed to an internal feed an Azure DevOps. There is no public access to this feeds at the moment.
The build environment for this project is on Azure DevOps and can be found here dev.azure.com/kirkone/KK.DotNet.BackgroundTasks.Scheduled
Name | Status |
---|---|
KK.DotNet.BackgroundTasks.Scheduled CI |
Name | Status |
---|---|
KK.DotNet.BackgroundTasks.Scheduled CD | |
Alpha | |
Beta | |
Release |
- Kirsten Kluge - Initial work - kirkone
- paule96 - Refactoring and understanding awesome interface stuff - paule96
- TiltonJH - Unbelievable knowledge about awesome interface stuff - TiltonJH
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details
- Inspired by this Microsoft documentation and this Blog post repo