Skip to content

No way to use base class for a job #4

@alex-training

Description

@alex-training

Hi @icsharp !

As long as there is no ExecuteAsync method support (hope yet) I tried to create a base class for my jobs.

    public abstract class BaseRecurringJob : IRecurringJob
    {
        public void Execute(PerformContext context)
        {
            ExecuteAsync(context).GetAwaiter().GetResult();
        }

        protected virtual Task ExecuteAsync(PerformContext context) => Task.CompletedTask;
    }

After that all my jobs can be used like so:
public class HealthCheckRecurringJob: BaseRecurringJob

There is a problem that a Hangfire JobActivator tries to create my base abstract class instead of using child class.

As I can see there is something wrong with this line:
option.JobType.GetMethod(nameof(IRecurringJob.Execute));

var addOrUpdate = Expression.Call(
				typeof(RecurringJob),
				nameof(RecurringJob.AddOrUpdate),
				new Type[] { method.DeclaringType },
				new Expression[]
				{
					Expression.Constant(recurringJobId),
					Expression.Lambda(methodCall, x),
					Expression.Constant(cron),
					Expression.Constant(timeZone),
					Expression.Constant(queue)
				});

Could you please try to fix this issue?

Br, Alex

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions