简体   繁体   中英

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
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM