Hi,
I’m trying to implement a plugin system of background tasks in an MVC application. The plugins implement an interface, and are not referenced in the MVC app. My goal is to have a dynamic plugin system where I can drop new plugins in the bin folder, and configure recurring jobs from these plugins via the MVC app.
The problem I have is that the Job is serialized as the interface, not as the actual class. Some code to add clarity:
public void AddOrUpdatePlugin(EnginePluginHangfireDto enginePluginHangfireDto)
{
var instance = ObjectFactory.GetInstance(enginePluginHangfireDto.Type);
RecurringJob.AddOrUpdate(
enginePluginHangfireDto.Code,
() => (instance as IEnginePlugin).ExecutePlugin(),
enginePluginHangfireDto.Cron);
}
This job gets serialized as
{"Type":"MyNamespace.IEnginePlugin, MyNamespace.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", ...
So when the job gets executed, there is an error: "cannot create an instance of an interface"
I tried to add the recurring job through reflection, but this serialized to the type MethodBase.
Is it possible to extend the RecurringJob class so it can get an extra method like this:
public static void AddOrUpdate(RuntimeMethodInfo methodInfo, string cronExpression)
{
var job = Job.FromRuntimeMethodInfo(methodInfo);
...
}
Or do you see another option without having to reference the plugin dll’s to the project?
Thanks,
Bart