在ASP.NET MVC Web应用程序基于队列的后台处理 [英] Queue-Based Background Processing in ASP.NET MVC Web Application

查看:114
本文介绍了在ASP.NET MVC Web应用程序基于队列的后台处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能实现后台处理队列在我的ASP.NET MVC Web应用程序?虽然大多数数据的变化,更新等需要立即可见,也有不需要,我想,随手开了一个低优先级的后台进程,将照顾它自己的速度实时处理其他更新

How can I implement background processing queues in my ASP.NET MVC web app? While most data changes, updates etc. need to be visible immediately, there are other updates that don't need real time processing which I would like to hand off to a lower-priority background process which will take care of it at its own pace.

作为一个例子,拿计算器的徽章奖励制度。通常你可能需要将授予你一个徽章一个具体行动,但实际的'奖'发生后(10分钟和几个小时后通常之间)。我想这是通过一个单独的后台进程完成的,因为它赚的时候并不重要了这么的工作机制,以奖励徽章立即生效。

As an example, take StackOverflow's badge award system. Usually you may take a specific action that would award you a badge, but the actual 'award' happens later (typically between 10 minutes and a couple hours later). I assume this is done via a separate background process as it is not critical for SO's workings to award badges immediately when earned.

所以,我试图创造某种排队系统中,我可以的东西的任务(说什么实现ITask接口,这将有一个过程()方法),这将最终会由独立的进程中执行。

So, I'm trying to create some kind of queue system in which I could stuff tasks (say anything implementing ITask interface which will have a Process() method) which will eventually get executed by a separate process.

我如何去实施这样一个系统?想法/提示/样品code?

How would I go about implementing such a system? Ideas/Hint/Sample Code?

感谢您!

推荐答案

Windows服务和MSMQ与他们沟通(如果你甚至需要)。

Windows Services and MSMQ to communicate with them (if you even need to).

- 编辑

要稍微扩大。

您将创建一些服务,这取决于你想要做什么,并让他们全部运行各种睡眠级别无尽的线程,做你想做的。然后,他们将适当地更新数据库,你不会有这样做在客户端什么。

You'll create several services, depending on what you want to do, and have them all run an endless thread of various sleeping levels, to do what you want. They will then update the database appropriately, and you will not have had to do anything on the client side.

您不妨从一个角度管理点与他们进行互动,因此你可能有一个MSMQ,他们听命令的管理上。根据您的实现,您可能需要重新启动他们出于某种原因,或者可能只是力的任何他们希望做一个的运行。

You may wish to interact with them from an admin point of view, hence you may have an MSMQ that they listen to admin commands on. Depending on your implementation, you may need to restart them for some reason, or possibly just 'force' a running of whatever they wish to do.

所以,你将使用一个MSMQ专用队列去做(System.Messaging命名空间)。其中一个主要的事情需要注意MSMQ,是信息必须是< 4meg。所以,如果你打算送大对象图,序列化到一个文件中第一和只发送文件名。

So you'll use an MSMQ Private Queue to do it (System.Messaging namespace). One of the main things to note about MSMQ, is that message need to be < 4meg. So if you intend to send large object graphs, serialise to a file first and just send the filename.

MSMQ是相当漂亮。您可以将基于相关ID如果你需要,但是,对于一些有趣的原因,相关ID必须是形式:

MSMQ is quite beautiful. You can send based on a 'Correlation ID' if you need to, but, for some amusing reason, the correlation ID must be in the form of:

{guid}\1

还有什么不工作(至少在该框架的2.0版本中,code可能有变化)。

Anything else does not work (at least in version 2.0 of the framework, the code may have changed).

- 编辑

例如,要求:

using System.Messaging;

...


MessageQueue queue = new MessageQueue(".\\Private$\\yourqueue");
queue.Formatter = new BinaryMessageFormatter();

Message m = new Message();
m.Body = "your serialisable object or just plain string";

queue.Send(m);


// on the other side

MessageQueue queue = new MessageQueue(".\\Private$\\yourqueue");
queue.Formatter = new BinaryMessageFormatter();

Message m = queue.Receive();

string s = m.Body as string;

// s contains that string now

这篇关于在ASP.NET MVC Web应用程序基于队列的后台处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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