Hangfire is a background class method runner and it's recurring job function RecurringJob.AddOrUpdate(Expression< Action >,string) is the method used to add methods to the queue. The first parameter is an Action call and the second is a cron formatted string.
If I have strings of the class and function name, how could I add a job.
Example normal non string call would be:
RecurringJob.AddOrUpdate(() => new MyClass().MyMethod(), "0 0 * * *");
I'd like to do something like
string myClassString = GetMyClassFromConfig();//value "MyNamespace.MyClass";
string myMethodString = GetMyMethodFromConfig();//value "MyMethod";
string myCronString = GetMyCronFromConfig();// value "0 0 * * *"
Type myType = Type.GetType(myClassString);
var myMethod = myType.GetMethod(myMethodString);
var myInstance = Expression.Parameter(myType,"instanceName");
RecurringJob.AddOrUpdate(Expression.Call(myInstance,myMethod), myCronString);
but this is throwing an error on the AddOrUpdate method call:
Could not create an instance of type System.Linq.Expressions.Expression. Type is an interface or abstract class and cannot be instantiated. Path 'Type', line 1, position 8.
How would I add jobs via string definitions or how would I make the Expression < Action > from strings that would allow the object instantiation and method running(new MyClass().Run()) shown in the upper sample?