Jishan
Jishan

Reputation: 199

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
}

Upvotes: 0

Views: 4804

Answers (2)

Mohammad Mokhtari
Mohammad Mokhtari

Reputation: 131

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

Upvotes: 3

Tom Houben
Tom Houben

Reputation: 51

I think it may be caused by a serialization issue. Take a look at this: Object getting Null seems like deserialization issue in Hangfire.

Short summary: Try to pass all the instance variables you use inside GetApiData() as parameters to this method.

Upvotes: 0

Related Questions