Skip to content

In-Memory Background Job

Note

In case of using the extension package TickerQ.EntityFrameworkCore you can skip this part and jump to the In-Database Background Job example.

Initial Setup

To set up a background job that runs periodically, follow these steps:

  1. Create a Public Instance Class
  2. Inherit from TickerController
  3. Annotate with TickerFunction Attribute
    • This attribute requires two parameters: FunctionName and CronExpression.

Example

Here's a simple example of a background job that will execute every 5 minutes. Not familiar with Cron Expressions? check out this Cronhub Tool.

csharp
public class MyFirstExample : TickerController
{
  [TickerFunction(FunctionName: "ExampleCronTicker", CronExpression: "*/5 * * * *")]
  public void ExampleCronTicker()
  {
    // Your background job logic goes here...
  }
}

Cron Expression Mapping

Map cron expression with the IConfiguration using %:

Update the attribute: TickerFunction

csharp
[TickerFunction(nameof(..., CronExpression: "%CronTicker:EveryMinute%")]

Define the cron expression to the Appsettings.json

json
[
 "CronTicker": {
   "EveryMinute": "* * * * *"
 }
]