在应用程序启动时设置Hangfire经常性作业时发生错误

时间:2018-08-30 14:57:08

标签: .net-core scheduled-tasks hangfire

我正在尝试在应用程序启动时(在.NET.Core Web应用程序的Startup.cs中)设置Hangfire经常性作业,以便我们可以确保它们总是已注册。我设法将其用于虚拟工作,但对于更真实的测试工作却遇到了一个奇怪的错误:

System.InvalidOperationException: Expression object should be not null.
   at Hangfire.Common.Job.FromExpression(LambdaExpression methodCall, Type explicitType)
   at Hangfire.RecurringJob.AddOrUpdate(String recurringJobId, Expression`1 methodCall, Func`1 cronExpression, TimeZoneInfo timeZone, String queue)
   at WsApplication.Web.Startup.Startup.ConfigureServices(IServiceCollection services) in C:\Projects\boxman\aspnet-core\src\WsApplication.Web.Host\Startup\Startup.cs:line 163
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

这是我的启动课程中的内容:

        // Hangfire (Enable to use Hangfire instead of default job manager)
        services.AddHangfire(config =>
        {
            config.UseSqlServerStorage(_appConfiguration.GetConnectionString("Default"));
        });
        JobStorage.Current = new SqlServerStorage(_appConfiguration.GetConnectionString("Default"));

        // Register recurring jobs on startup
        var sp = services.BuildServiceProvider();
        var offHireJob = sp.GetService<UnsetContainerIsBookedPerOffHireJob>();
        // Test job 1 - this gets registered fine!
        RecurringJob.AddOrUpdate("Test", () => Console.Write("Test"), Cron.Minutely);
        // Test job 2 - This line triggers the error!
        RecurringJob.AddOrUpdate(UnsetContainerIsBookedPerOffHireJob.JobId, () => offHireJob.Run(), Cron.Minutely);

这是我的实际测试工作:

public class UnsetContainerIsBookedPerOffHireJob : ApplicationService
{
    public readonly static string JobId = nameof(UnsetContainerIsBookedPerOffHireJob);
    private readonly IRepository<ContractLineMovement, int> _contractLineMovementRepository;

    public UnsetContainerIsBookedPerOffHireJob(
        IRepository<ContractLineMovement, int> contractLineMovementRepository
    ){ 
        _contractLineMovementRepository = contractLineMovementRepository;
    }

    public void Run()
    {
        Logger.Debug("Running UnsetContainerIsBookedPerOffHireJob.Run()");

        var offHirequery = from contractLineMovement in _contractLineMovementRepository.GetAll()
                           where contractLineMovement.ContractWorkFlowEventId == (int)ContractWorkFlowEventValues.OffHire
                           select contractLineMovement;

        foreach (var offHire in offHirequery)
        {
            Logger.Debug("Processing offhire with ID: " + offHire.Id + " and Date: " + offHire.HireDate);
        }
    }
}

我发现the piece of code in Hangfire's source code引发了此异常,但我仍然不太明白为什么该表达式

  

()=> offHireJob.Run()

会引起问题。在我看来,这很基本。也许我缺少一些基本的东西?

还是有更好的方法可以在应用程序级别一次注册我的周期性工作?

0 个答案:

没有答案