'Job' 字段在 Hangfire 中具有 null 或空值

The 'Job' field has a null or empty value in Hangfire

在 Startup.cs 中,我尝试像这样对重复作业进行排队:

RecurringJob.AddOrUpdate(() => Console.WriteLine("test"), Cron.Daily);

但收到错误:

请帮助找出我做错了什么。

我的配置:

    //HangFire
    services.AddHangfire(configuration => configuration
      .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
      .UseSimpleAssemblyNameTypeSerializer()
      .UseRecommendedSerializerSettings()
      .UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection"),
          new SqlServerStorageOptions
          {
              CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
              SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
              QueuePollInterval = TimeSpan.Zero,
              UseRecommendedIsolationLevel = true,
              DisableGlobalLocks = true,
          }
      ));

P.S。尽管 'fire and forget' 工作有效。

Hangfire's source表示当表达式!recurringJob.ContainsKey("Job") || String.IsNullOrWhiteSpace(recurringJob["Job"])为真时抛出。

try
{
    if (!recurringJob.ContainsKey("Job") || String.IsNullOrWhiteSpace(recurringJob["Job"]))
    {
        throw new InvalidOperationException("The 'Job' field has a null or empty value");
    }

    Job = InvocationData.DeserializePayload(recurringJob["Job"]).DeserializeJob();
}
catch (Exception ex)
{
    _errors.Add(ex);
}

recurringJob 词典由 method GetAllEntriesFromHash:

设置
public override Dictionary<string, string> GetAllEntriesFromHash(string key)
{
    if (key == null) throw new ArgumentNullException(nameof(key));

    return _storage.UseConnection(_dedicatedConnection, connection =>
    {
        var result = connection.Query<SqlHash>(
            $"select Field, Value from [{_storage.SchemaName}].Hash with (forceseek, readcommittedlock) where [Key] = @key",
            new { key },
            commandTimeout: _storage.CommandTimeout)
            .ToDictionary(x => x.Field, x => x.Value);

        return result.Count != 0 ? result : null;
    });
}

所以可能发生的情况是 GetAllEntriesFromHash 方法返回 null, 或不包含键 Job(或 null/whitespace)的字典。但是,在 a thread 中,一位 Hangfire 贡献者评论道:

That's not even a problem, just ignore those exceptions or tell Visual Studio to not to break on them. Hangfire handles a lot of exceptions, and may generate tons of OperationCanceledException during shutdown that tells all the background processes that shutdown was requested.

因此,这个错误似乎可以忽略。