如何在 Hangfire 中调用异步方法? [英] How to invoke async methods in Hangfire?

查看:93
本文介绍了如何在 Hangfire 中调用异步方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 asp.net 核心 API 应用程序,这是我第一次使用 HangFire.

在 .Net Core 应用程序中,我的所有方法都是异步的.基于 SO Post 它不是在 Hangfire 中调用异步方法时使用 wait() 的好主意.
此外,根据 v1.6.0 中的 hangfire 支持问题,添加了异步支持.我使用的是 1.6.12 版,但仍然看不到异步支持.

我如何从 Enqueue 调用异步方法.目前我正在使用 wait()

public class MyController : Controller{私有只读下载器_downlaoder;私有只读 IBackgroundJobClient _backgroungJobClient;public MyController(下载器下载器,IBackgroundJobClient backgroungJobClient){_downlaoder = 下载器;_backgroungJobClient = backgroungJobClient;}[HttpPost]public void Post([FromBody]IEnumerable files){_backgroungJobClient.Enqueue(() => _downloader.DownloadAsync(files).Wait());}}

解决方案

基于 github 上的存储库

只需移除 Wait 阻塞调用

_backgroungJobClient.Enqueue(() => _downloader.DownloadAsync(files));

该方法现在知道如何处理返回 Task 的 Func

Hangfire 1.6.0 - 博客<块引用>

同步和异步方法的入队逻辑是相同的.在早期测试版有一个警告 CS4014,但现在你可以删除所有#pragma 警告禁用语句.它是通过使用 Expression> 参数重载实现的.

BackgroundJob.Enqueue(() => HighlightAsync(snippet.Id));

注意:

<块引用>

这不是真正的异步

请将此功能视为一种语法糖.背景处理没有变成异步.内部已实施使用 Task.Wait 方法,因此工作人员不执行任何处理,在等待任务完成时.真正的异步可能会出现仅限 Hangfire 2.0,它需要对现有类型.

I have asp.net core API application and this is the first time i will be using HangFire.

In .Net Core application all my methods are async. Based on SO Post it's not good idea to use wait() while calling async method in hangfire.
Also as per the hangfire support issue in v1.6.0, async support was added. I am using version 1.6.12 but still i dont see async support.

How do i call async method from Enqueue. Currently i am using wait()

public class MyController : Controller
{
    private readonly Downloader _downlaoder;
    private readonly IBackgroundJobClient _backgroungJobClient;
    public MyController(Downloader downloader, IBackgroundJobClient backgroungJobClient)
    {
        _downlaoder = downloader;
        _backgroungJobClient = backgroungJobClient;
    }

    [HttpPost]
    public void Post([FromBody]IEnumerable<string> files)
    {
        _backgroungJobClient.Enqueue(() => _downloader.DownloadAsync(files).Wait());
    }
}

解决方案

Based on one of the examples on the repository on github

Just remove the Wait blocking call

_backgroungJobClient.Enqueue(() => _downloader.DownloadAsync(files));

The method knows now how to handle Func that returns Task

Hangfire 1.6.0 - Blog

The enqueueing logic is the same for sync and async methods. In early betas there was a warning CS4014, but now you can remove all the #pragma warning disable statements. It was implemented by using Expression<Func<Task>> parameter overloads.

BackgroundJob.Enqueue(() => HighlightAsync(snippet.Id));

Note:

That’s not a real asynchrony

Please consider this feature as a syntactic sugar. Background processing hasn’t became asynchronous. Internally it was implemented using the Task.Wait method, so workers don’t perform any processing, while waiting for a task completion. Real asynchrony may come in Hangfire 2.0 only, and it requires a lot of breaking changes to the existing types.

这篇关于如何在 Hangfire 中调用异步方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆