Search code examples
c#asp.netasp.net-core-2.0background-processhangfire

How to call another method using hangfire recurring Job?


I have implemented a method name GetApiData(). I want to call this method per minute using hangfire recurring job. But when I call this method it's showing the error

System.ArgumentNullException: Value cannot be null.
Parameter name: method
   at Hangfire.Common.Job..ctor(Type type, MethodInfo method, Object[] args)
   at Hangfire.Common.Job.FromExpression(LambdaExpression methodCall, Type explicitType)
   at Raasforce.Crm.ContactRepositoriesAppService.APIDataBackgroundJob() in E:\RaasforceCorp\RaasforceCorp\aspnet-core\src\Raasforce.Application\Crm\ContactRepositoriesAppService.cs:line 795
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.<>c__DisplayClass33_0.<WrapVoidMethod>b__0(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.VoidResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()

What will be the proper syntax? It is showing run time error.

public void APIDataBackgroundJob()
{
    RecurringJob.AddOrUpdate(()=>GetApiData(),Cron.Minutely);//This line is showing error
}

Solution

  • you should use like this

    public void APIDataBackgroundJob()
    {
        RecurringJob.AddOrUpdate<YourClassContainsThisMethod>(service=>service.GetApiData(),Cron.Minutely);//This line is showing error
    }
    

    and be aware you can only use public methods