Register a class instance (with populated public properties) as a recurring task

recurring
Tags: #<Tag:0x00007f64b68dd778>

#1

Maybe I’m missing something obvious but here goes …

In my system I have all kinds of “tasks”. They are basically classes which implement an interface, called ITask which looks like this:

public interface ITask
{
       void Run();
}

If the tasks need additional configuration, they can have public properties which the system exposes to the user when the task is being configured, so the user gets a UI where he can populate the properties of the task.

Here’s an example of a task:

public class TestTask : ITask
{
   [EditorBrowsable]
   public string TextToDisplay { get; set; }

   public void Run()
   {
       Console.WriteLine(TextToDisplay);
   }
}

The task config is then persisted to my database along with a dictionary representing all the public properties and their values. Then I have a factory method that can create instances of these tasks and populate all the stored properties, by reading the config from the database. What I end up with is an instance of the task implementation with all the public properties populated correctly. Essentially just the same as doing this in code:

var recurringTask = new TestTask() { TextToDisplay = “Foobar” };

How can I schedule a RecurringJob in Hangfire to take this recurringTask instance (with all public properties populated) and call the Run() method on it?