简体   繁体   English

ASP.NET中的HostingEnvironment.QueueBackgroundWorkItem()用于小型后台任务

[英]HostingEnvironment.QueueBackgroundWorkItem() in ASP.NET for small background tasks

I came a across a nice little tool that has been added to ASP.NET in v4.5.2 我来了一个很好的小工具,已经在v4.5.2中添加到了ASP.NET中

I am wandering how safe it is and how one can effectively utilize it in an ASP.NET MVC or Web API scenario. 我正在徘徊它是多么安全以及如何在ASP.NET MVC或Web API场景中有效地利用它。

I know I am always wanting to do a quick and simple fire and forget task in my web applications. 我知道我总是希望在我的Web应用程序中快速简单地执行任务并忘记任务。 For example: 例如:

  • Sending an emails/s 发送电子邮件
  • Sending push notifications 发送推送通知
  • Logging analytics or errors to the db 将分析或错误记录到数据库

Now typically I just create a method called 现在通常我只是创建一个名为的方法

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

and I would use it like so: 我会像这样使用它:

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"));

however when reading up about this. 但是在阅读这篇文章的时候。 It is apparently not the best thing to do in IIS environment. 在IIS环境中显然不是最好的事情。 (i'm not 100% sure on the specifics). (我不是100%确定细节)。

So this is where I came across HostingEnvironment.QueueBackgroundWorkItem(x=> SendEmailAsync(User.Identity.Username, "Hello")); 所以这就是我遇到的HostingEnvironment.QueueBackgroundWorkItem(x=> SendEmailAsync(User.Identity.Username, "Hello"));

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. 这是一种非常快速简便的方法,可以将发送电子邮件任务卸载到后台工作程序,并更快地为用户View()提供服务。

Now I am aware this is not for tasks running longer than 90 seconds and is not 100% guaranteed executution. 现在我知道这不适用于运行时间超过90 seconds且不是100%保证执行的任务。

But my question is: 但我的问题是:

Is HostingEnvironment.QueueBackgroundWorkItem() sufficient for: sending emails, push notifications, db queries etc in a standard ASP.NET web site. HostingEnvironment.QueueBackgroundWorkItem()足以用于:在标准ASP.NET网站中发送电子邮件,推送通知,数据库查询等。

It depends. 这取决于。


The main benefit of QueueBackgroundWorkItem is the following, emphasis mine ( source ): QueueBackgroundWorkItem的主要好处如下,强调我的( ):

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. 与普通的ThreadPool工作项不同,ASP.NET可以跟踪通过此API注册的工作项当前正在运行的数量,并且ASP.NET运行时将尝试延迟AppDomain关闭,直到这些工作项完成执行。

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 . 既不应该花费很长时间,因此可以在QueueBackgroundWorkItem运行。

However, there's no guarantee for the task to finish — as you said, the task is not await ed. 但是,没有任何保证完成任务 - 正如你所说,任务没有await It all depends on the importance of the task to execute. 这一切都取决于执行任务的重要性。 If the task must complete, it's not a good candidate for QueueBackgroundWorkItem . 如果任务必须完成,那么它不适合QueueBackgroundWorkItem

暂无
暂无

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

相关问题 HostingEnvironment.QueueBackgroundWorkItem 永远不会在 azure 部署的 asp.net mvc 4.5 应用程序上执行 - HostingEnvironment.QueueBackgroundWorkItem never executes on azure deployed asp.net mvc 4.5 application HostingEnvironment.QueueBackgroundWorkItem支持多个后台进程 - HostingEnvironment.QueueBackgroundWorkItem support for multiple background processs WCF 中带有 HostingEnvironment.QueueBackgroundWorkItem 的后台线程 - Background thread with HostingEnvironment.QueueBackgroundWorkItem in WCF .NET Core 中 HostingEnvironment.QueueBackgroundWorkItem 的替代解决方案 - Alternative solution to HostingEnvironment.QueueBackgroundWorkItem in .NET Core HostingEnvironment.QueueBackgroundWorkItem - 澄清? - HostingEnvironment.QueueBackgroundWorkItem - Clarification? HostingEnvironment.QueueBackgroundWorkItem真的会延迟回收吗? - Does HostingEnvironment.QueueBackgroundWorkItem really delay recycling? HostingEnvironment.QueueBackgroundWorkItem中的事务范围超时 - Transaction scope in HostingEnvironment.QueueBackgroundWorkItem time out HostingEnvironment.QueueBackgroundWorkItem和HostingEnvironment.RegisterObject之间的区别 - Difference between HostingEnvironment.QueueBackgroundWorkItem and HostingEnvironment.RegisterObject 在一定时间后强制取消HostingEnvironment.QueueBackgroundWorkItem - Forcing HostingEnvironment.QueueBackgroundWorkItem to cancel after a certain time ASP.NET /调试中的后台任务 - Background Tasks in ASP.NET / Debug
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM