Recurring Job That Executes a Generic Type Function

public interface IJob<T> //Job interface
{
    Task Execute<U>();
}

public abstract class Job<T> : IJob<T> // base job class
{
    public async Task Execute<U>()
    {
        object item = new object();
        BackgroundJob.Enqueue<IWorkerService<U>>(x => x.WorkerTask(item));
    }
}

public class MyJob: Job<MyJob> // a job
{
    //some other stuff
}

public class MyWorkerService: IWorkerService<MyWorkerService>
{
    public async Task WorkerTask(object id)
    {
        //some implementation
    }
}

public interface IWorkerService<T>
{
    Task WorkerTask(object item);
}

// I register the class as this
services.AddScoped<IJob<MyJob>, MyJob>();
//services.AddScoped<IJob<MyJob2>, MyJob2>();
services.AddScoped<IWorkerService<MyWorkerService>, MyWorkerService>();
//services.AddScoped<IWorkerService<MyWorkerService2>, MyWorkerService2>();

// register recurring myjob as recurring job
RecurringJob.AddOrUpdate<IJob<OutboxInvoiceTaskJob>>("myJob", c => c.Execute<MyWorkerService>(), Cron.Minutely(), TimeZoneInfo.Local);
//RecurringJob.AddOrUpdate<IJob<OutboxInvoiceTaskJob>>("myJob2", c => c.Execute<MyWorkerService2>(), Cron.Minutely(), TimeZoneInfo.Local);

What I am trying in here is to create a base job that can be extended easily as recurring jobs and these recurring jobs create background jobs based on the given type, however I get this exception:

System.ArgumentNullException: Value cannot be null.
    at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
    at Hangfire.Common.TypeExtensions.GetNonOpenMatchingMethod(Type type, String name, Type[] parameterTypes)
    at Hangfire.Storage.InvocationData.Deserialize()
1 Like

Can anyone help about this issue please?

Hangfire doesn’t support generic types.

Do multiple functions without generic type for every job and inside call the generic one.