简体   繁体   English

QueueBackgroundWorkItem是否实际上将后台工作排队?

[英]Does QueueBackgroundWorkItem actually queue background work?

We're running an ASP.NET WebAPI 2 service and we want to log some requests with our logger to email/database. 我们正在运行ASP.NET WebAPI 2服务,我们希望使用记录器将一些请求记录到电子邮件/数据库中。

Because it's background work, and because in asp.net I figured we should use HostingEnvironment.QueueBackgroundWorkItem to run it in the background. 因为它是后台工作,并且因为我认为在asp.net中,我们应该使用HostingEnvironment.QueueBackgroundWorkItem在后台运行它。

I want my logs to all be in order - to my surprise I could not find anything indicating that QueueBackgroundWorkItem actually guarantees that the queued work items run in order or indicates it doesn't. 我希望所有日志都井井有条-令我惊讶的是,我找不到任何表明QueueBackgroundWorkItem实际上保证排队的工作项按顺序运行或表明没有按顺序运行的信息。

So, my question is: Does QueueBackgroundWorkItem guarantee that work queued gets executed in order? 所以,我的问题是: QueueBackgroundWorkItem是否保证排队的工作按顺序执行?

 HostingEnvironment.QueueBackgroundWorkItem((e) => Console.WriteLine("A")); 
 HostingEnvironment.QueueBackgroundWorkItem((e) => Console.WriteLine("B"));

Do I know that the output of the above snippet is always: 我是否知道上面代码段的输出始终是:

A
B

Or can it be out of order? 还是会出现故障?

There appears to be nothing contractual in the documentation. 文档中似乎没有任何契约性内容。

Looking at the reference source, it appears to use a class called BackgroundWorker to actually execute these tasks. 查看参考源,似乎使用了一个名为BackgroundWorker的类来实际执行这些任务。

In turn, this appears to be running the tasks on the ThreadPool and explicitly may be executing multiple tasks in parallel: 反过来,这似乎正在ThreadPool上运行任务,并且明确地可能并行执行多个任务:

    public void ScheduleWorkItem(Func<CancellationToken, Task> workItem) {
        Debug.Assert(workItem != null);

        if (_cancellationTokenHelper.IsCancellationRequested) {
            return; // we're not going to run this work item
        }

        // Unsafe* since we want to get rid of Principal and other constructs specific to the current ExecutionContext
        ThreadPool.UnsafeQueueUserWorkItem(state => {
            lock (this) {
                if (_cancellationTokenHelper.IsCancellationRequested) {
                    return; // we're not going to run this work item
                }
                else {
                    _numExecutingWorkItems++;
                }
            }

            RunWorkItemImpl((Func<CancellationToken, Task>)state);
        }, workItem);
    }

So I'd say it's unsafe to assume anything about what order two queued tasks will complete in. 因此,我想假设两个排队的任务将以什么顺序完成是不安全的。

QueueBackgroundWorkItem guarantee that work queued gets executed in order QueueBackgroundWorkItem保证排队的工作按顺序执行

Reference from HostingEnvironment.QueueBackgroundWorkItem 来自HostingEnvironment.QueueBackgroundWorkItem的引用

New HostingEnvironment.QueueBackgroundWorkItem method that lets you schedule small background work items. 新的HostingEnvironment.QueueBackgroundWorkItem方法,可用于安排小型后台工作项。 ASP.NET tracks these items and prevents IIS from abruptly terminating the worker process until all background work items have completed. ASP.NET跟踪这些项目,并防止IIS突然终止工作进程,直到所有后台工作项目均已完成。 These will enable ASP.NET applications to reliably schedule Async work items. 这些将使ASP.NET应用程序能够可靠地调度异步工作项。

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

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