调用 Hangfire BackgroundJob.Enqueue 方法时应该使用异步包装器吗?

Should I use an async wrapper when calling the Hangfire BackgroundJob.Enqueue method?

将 Enqueue 方法放在异步包装器中并让控制器以这种方式调用它是个好主意吗?

哪个更好,为什么?

同步:

    public IActionResult AddLogs(List<Log> logs)
    {
        BackgroundJob.Enqueue(() => DM.AddLogsBackground(logs));
        return Ok();
    }

异步:

    public async Task<IActionResult> AddLogs(List<Log> logs)
    {
        await DM.AddLogs(logs);
        return Ok();
    }

    public async Task AddLogs(List<Log> logs)
    {
        BackgroundJob.Enqueue(() => AddLogsBackground(logs));
    }

documentation 说:

The Enqueue method does not call the target method immediately, it runs the following steps instead:

  1. Serialize a method information and all its arguments.
  2. Create a new background job based on the serialized information.
  3. Save background job to a persistent storage.
  4. Enqueue background job to its queue.

Which is better and why?

由于 BackgroundJob.Enqueue 未等待(也不可等待),因此将被同步处理。

所以所有的异步语法在这里都是无用和误导的。

注意你的 DM.AddLogsBackground can be an async method。但据我所知,处理仍然会在后台作业服务器上同步。