【问题标题】:How to call another method using hangfire recurring Job?如何使用hangfire重复作业调用另一种方法?
【发布时间】:2019-04-23 12:24:59
【问题描述】:

我已经实现了一个方法名GetApiData()。我想使用hangfire 重复作业每分钟调用一次此方法。但是当我调用这个方法时,它会显示错误

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()

什么是正确的语法?它显示运行时错误。

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

【问题讨论】:

  • 试试var myInstance= new MyClass(); RecurringJob.AddOrUpdate(myInstance.GetApiData,Cron.Minutely);
  • 你能分享堆栈跟踪或GetApiData 方法做什么?
  • 它一次需要 5 个数据从另一个 api 插入到数据库 @Aarif 的不同表中
  • 没有代码,错误是没有意义的。投票结束。
  • 尝试提供方法的引用而不是调用它()=&gt;GetApiData

标签: c# asp.net asp.net-core-2.0 background-process hangfire


【解决方案1】:

你应该这样使用

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

请注意,您只能使用公共方法

【讨论】:

    【解决方案2】:

    我认为这可能是由序列化问题引起的。看看这个:Object getting Null seems like deserialization issue in Hangfire

    简短的总结: 尝试将您在GetApiData() 中使用的所有实例变量作为参数传递给此方法。

    【讨论】:

      猜你喜欢
      • 2018-02-23
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 2021-11-10
      • 1970-01-01
      相关资源
      最近更新 更多