ASP.NET中的HostingEnvironment.QueueBackgroundWorkItem()用于小型后台任务 [英] HostingEnvironment.QueueBackgroundWorkItem() in ASP.NET for small background tasks

查看:78
本文介绍了ASP.NET中的HostingEnvironment.QueueBackgroundWorkItem()用于小型后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个不错的小工具,该工具已在v4.5.2中添加到ASP.NET中

I came a across a nice little tool that has been added to ASP.NET in v4.5.2

我一直在徘徊它的安全性以及如何在ASP.NET MVC或Web API方案中有效地利用它.

I am wandering how safe it is and how one can effectively utilize it in an ASP.NET MVC or Web API scenario.

我知道我一直想在我的Web应用程序中做一个快速简单的事情,忘记任务.例如:

I know I am always wanting to do a quick and simple fire and forget task in my web applications. For example:

  • 发送电子邮件/秒
  • 发送推送通知
  • 将分析或错误记录到数据库

现在通常我只创建一个名为

Now typically I just create a method called

public async Task SendEmailAsync(string to, string body)
{
 //TODO: send email
}

我会这样使用它:

public async Task<ActionResult> Index()
{
 ...
 await SendEmailAsync(User.Identity.Username, "Hello");
return View(); 
}

现在我对此的关注是,我正在延迟用户以便向他们发送电子邮件.这对我来说没有多大意义.

now my concern with this is that, I am delaying the user in order to send my email to them. This doesn't make much sense to me.

所以我首先考虑做一下:

So I first considered just doing:

Task.Run(()=> SendEmailAsync(User.Identity.Username, "Hello"));

但是,当阅读有关此内容时.在IIS环境中,这显然不是最好的选择.(我不确定100%的细节).

however when reading up about this. It is apparently not the best thing to do in IIS environment. (i'm not 100% sure on the specifics).

所以这是我遇到的 HostingEnvironment.QueueBackgroundWorkItem(x => SendEmailAsync(User.Identity.Username,"Hello"));

这是将发送电子邮件任务分派给后台工作人员并为用户 View()更快地提供服务的一种非常快速简便的方法.

This is a very quick and easy way to offload the send email task to a background worker and serve up the users View() much quicker.

现在,我知道这不适用于运行时间超过 90秒的任务,并且不能100%保证执行.

Now I am aware this is not for tasks running longer than 90 seconds and is not 100% guaranteed executution.

但是我的问题是:

HostingEnvironment.QueueBackgroundWorkItem()足以用于:在标准ASP.NET网站中发送电子邮件,推送通知,数据库查询等.

Is HostingEnvironment.QueueBackgroundWorkItem() sufficient for: sending emails, push notifications, db queries etc in a standard ASP.NET web site.

推荐答案

这要视情况而定.

QueueBackgroundWorkItem 的主要优点如下:(

The main benefit of QueueBackgroundWorkItem is the following, emphasis mine (source):

与普通ThreadPool工作项的不同之处在于,ASP.NET可以跟踪当前正在通过此API注册的工作项的数量,并且 ASP.NET运行时将尝试延迟AppDomain关闭直到这些工作项执行完毕.

Differs from a normal ThreadPool work item in that ASP.NET can keep track of how many work items registered through this API are currently running, and the ASP.NET runtime will try to delay AppDomain shutdown until these work items have finished executing.

本质上, QueueBackgroundWorkItem 通过尝试在仍有任务运行时不关闭应用程序来帮助您运行可能花费几秒钟的任务.

Essentially, QueueBackgroundWorkItem helps you run tasks that might take a couple of seconds by attempting not to shutdown your application while there's still a task running.

运行普通的数据库查询或发出推式通知应该花费几百毫秒(或几秒钟)的时间;两者都不需要花费很长时间,因此可以在 QueueBackgroundWorkItem 中运行.

Running a normal database query or sending out a push notification should be a matter of a couple hundred milliseconds (or a few seconds); neither should take a very long time and should thus be fine to run within QueueBackgroundWorkItem.

但是,不能保证任务会完成-正如您所说,任务没有 await .这完全取决于要执行的任务的重要性.如果必须完成任务,则不是 QueueBackgroundWorkItem 的理想选择.

However, there's no guarantee for the task to finish — as you said, the task is not awaited. It all depends on the importance of the task to execute. If the task must complete, it's not a good candidate for QueueBackgroundWorkItem.

这篇关于ASP.NET中的HostingEnvironment.QueueBackgroundWorkItem()用于小型后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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