简体   繁体   English

MediatR NotificationHandler失控

[英]MediatR NotificationHandler fire and forget

How can I make my MediatR notification handler fire and forget? 如何使MediatR通知处理程序触发并忘记?

My sample code: 我的示例代码:

public class BackupDatabase : INotification {}

public class BackupDatabaseNotificationHandler : INotificationHandler<BackupDatabase>
{
    public async Task Handle(BackupDatabase notification, CancellationToken cancellationToken)
    {
        await Task.Delay(100000000);
    }
}

public async Task<IActionResult> SomeAction()
{
    await _mediator.Publish(new BackupDatabase());
    return Json(true);
}

I know one way is to use the Task.Factory.StartNew, But is there another way? 我知道一种方法是使用Task.Factory.StartNew,但是还有另一种方法吗?

Mediatr notification handlers are already fire and forget. Mediatr通知处理程序已经被抛弃了。

Another way to deal with this would be to queue a background task to do the work. 解决此问题的另一种方法是将后台任务排队以完成工作。 Look at something like Hangfire and implement that in either a _mediator.Send() or _mediator.Publish() method. 看一下类似于Hangfire的东西,并通过_mediator.Send()或_mediator.Publish()方法实现它。

Hangfire will give you the ability to monitor the background task and has the ability to automatically retry the task if required. Hangfire将使您能够监视后台任务,并能够根据需要自动重试该任务。

There are some other options if Hangfire isn't an option for you. 如果您不适合使用Hangfire,还有其他选择

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM