hangfire定时任务 方法未定义

废话少说,直接上代码:

/// <summary>
/// 开启定时联动定时任务
/// </summary>
public void StartTimingTouchLinkAsync()
{
    string recurringJobId = "设备联动定时执行";
    string cronStr = "*/5 * * * * *";
    RecurringJob.AddOrUpdate(recurringJobId,this.TimingTouchLinkAsync(), cronStr, TimeZoneInfo.Local);
}

/// <summary>
/// 定时触发(每5秒) 联动触发(从关系库获取数据)
/// </summary>        
/// <returns></returns>
[UnitOfWork]
public virtual async Task TimingTouchLinkAsync()
{
    /***省略部分代码 ***/
   var isMeet = await IsMeetCondition(conditionSymbol, conditionValue, intimeValue);
    /***省略部分代码 ***/
   //触发执行联动
    await this.DoLinkAsync(currentLinkId);
    /***省略部分代码 ***/
}

 

定时任务执行结果:

 

 

System.InvalidOperationException: Recurring job can't be scheduled, see inner exception for details.
 ---> Hangfire.Common.JobLoadException: Could not load the job. See inner exception for the details.
 ---> System.InvalidOperationException: The type `Awing.IBMS.Application.Business.LinkAppService` does not contain a method with signature `TimingTouchLinkAsync()`
   at Hangfire.Storage.InvocationData.DeserializeJob()
   --- End of inner exception stack trace ---
   at Hangfire.Storage.InvocationData.DeserializeJob()
   at Hangfire.RecurringJobEntity..ctor(String recurringJobId, IDictionary`2 recurringJob, ITimeZoneResolver timeZoneResolver, DateTime now)
   --- End of inner exception stack trace ---
   at Hangfire.Server.RecurringJobScheduler.ScheduleRecurringJob(BackgroundProcessContext context, IStorageConnection connection, String recurringJobId, RecurringJobEntity recurringJob, DateTime now)

 

原因分析:

明明有TimingTouchLinkAsync()方法,错误提示却还是不存在该方法,应该是方法中出现了错误。

因为TimingTouchLinkAsync()方法中同步方法、异步方法并存,故该方法可能出现同步异步执行时间混乱问题。

解决办法是为该方法。

1.方法添加属性 UnitOfWork

2.方法采用虚方法 virtual

3.注意方法中调用的其他方法也要采用上述2条。

 

 

 关联阅读> hangfire报错 Could not load file or assembly 'DynamicProxyGenAssembly2, Culture=neutral, PublicKeyToken=null'. 系统找不到指定的文件

 

posted @ 2021-02-22 17:52  深海地平线  阅读(1823)  评论(3编辑  收藏  举报