如何在排队的ASP.NET Web API后台任务 [英] How to queue background tasks in ASP.NET Web API

查看:207
本文介绍了如何在排队的ASP.NET Web API后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设计来处理队列的方式报告的WebAPI。是应用程序需要的步骤如下:

I have a webapi that is designed to process reports in a queue fashion. The steps the application takes are as follows:


  • 接收内容

  • 内容映射到一个对象,并将其放置到队列

  • 轮询队列中挂起项目

  • 在一个时间在队列中的一个过程项

我想用实体框架来创建排队的物品,如数据库:

I was thinking to use Entity Framework to create a database of queued items, such as:

public class EFBatchItem
{
    [Key]
    public string BatchId { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateCompleted { get; set; }
    public string BatchItem { get; set; }
    public BatchStatus Status { get; set; }
}

我的问题 - 是否有更有效的方法,用NServiceBus,BlockingCollection或ConcurrentQeueue,而不是不断查询数据库,并拉出挂起项目一个接一个?我以前没有使用队列。

My question - Is there a more efficient way, using NServiceBus, BlockingCollection or ConcurrentQeueue, than to constantly poll the database and pull out pending items one by one? I have not used queues before.

一个想法是创建一个任务队列,并在一个单独的线程处理所有未决的任务。有点类似<一个href=\"http://stackoverflow.com/questions/6203836/most-efficient-way-to-process-a-queue-with-threads\">Most有效的方式来与,但我想确保我会是最有效的路线线程处理队列中。

One thought is to create a queue of tasks, and on a separate thread process all pending tasks. Somewhat similar to Most efficient way to process a queue with threads but I want to ensure that I am going the most efficient route.

编辑:
一个大问题,我这里是显示进度给用户的最佳方式。一旦用户提交的内容,他被带到一个新的页面,可以查看批次标识状态。 MSMQ是必要的,或NServiceBus,以通知用户?这似乎是请求/应答/推范式的变化?

A big question I have here is the best way to display the progress to the user. Once the user submits content, he gets taken to a new page and can view the status by the batch identifier. Is MSMQ necessary, or NServiceBus, in order to notify the user? This seems like a variation of the REquest/Acknowledge/Push paradigm?

推荐答案

恕我直言,你的ASP.NET Web API应用程序不应该由自己运行这些后台任务。它应该只负责接收请求,把它粘在队列内(如你表示),并返回指示接收到的消息的成功或失败的响应。您可以使用多种邮件系统,如的RabbitMQ 这种方法。

IMHO, your ASP.NET Web API application shouldn't run those background tasks by itself. It should only be responsible to receive the request, stick it inside the queue (as you indicated) and return the response indicating the success or failure of the received message. You can use variety of messaging systems such as RabbitMQ for this approach.

对于通知,您有几种选择。可以有一个端点,客户端可以检查处理是否完成与否。或者,你可以提供一个流API端点,您的客户端可以订阅。通过这种方式,客户端没有轮询服务器;服务器可以通知被连接的客户端。的ASP.NET Web API有这样一个伟大的方式。下面的博客文章介绍如何:

As for the notification, you have a few options. You can have an endpoint which the client can check whether the processing is completed or not. Alternatively, you could provide a streaming API endpoint which your client can subscribe to. This way, the client doesn't have to poll the server; the server can notify the clients which are connected. ASP.NET Web API has a great way of doing this. The following blog post explains how:

  • Native HTML5 push notifications with ASP.NET Web API and Knockout.js

您也可以考虑 SignalR 这种类型的服务器到客户端的通知。

You can also consider SignalR for this type of server to client notifications.

为什么后台任务是很难的ASP.NET Web API应用程序的原因是,你有责任保持AppDomain中存活。这尤其是当你在IIS托管麻烦。下面的博客文章解释了真正的好我的意思是:

The reason why background tasks are hard for ASP.NET Web API application is that you're responsible to keep the AppDomain alive. This is a hassle especially when you are hosting under IIS. The following blog posts explains really good what I mean:

  • Returning Early from ASP.NET Requests
  • The Dangers of Implementing Recurring Background Tasks In ASP.NET

这篇关于如何在排队的ASP.NET Web API后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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