ASP.Net Web API中的消息队列

[英]Message queues in ASP.Net Web API

I am developing a client-side single-page-application (SPA) with AngularJS and ASP.Net WebAPI.

我正在使用AngularJS和ASP.Net WebAPI开发客户端单页面应用程序(SPA)。

One of the features of the SPA includes uploading large CSV file, processing it on the server, and returning the output to the user.


Obviously, this kind of computation can not be done online, and therefore I implemented an UploadController in charge of receiving the file, and a PollingController in charge of notifying the user when the computation is complete.


The client side application monitors the PollingController every few seconds.


I have no experience in Message Queues, but my gut tells me that they are required in this situation.


How would you recommend to implement this functionality in a non-blocking, efficient way ?



Examples will be highly appreciated


2 个解决方案



I've used message based service bus frameworks for this in the past.


You write an application (running as a windows service), that listens for messages broadcast across a event bus.


Your frontend can publish these messages into the bus.


The most popular framework for this in .NET is NServiceBus, however it recently became commercial. You can also look into MassTransit, though this one has very poor documentation.


The workflow you would do:


  • MVC App accepts upload and places it into some directory accessible by the windows service
  • MVC App接受上传并将其放入windows服务可访问的某个目录中
  • MVC App publishes "UploadReady" message.
  • MVC App发布“UploadReady”消息。
  • Service receives message, processes file, and updates some state for the polling controller.
  • 服务接收消息,处理文件,并更新轮询控制器的某些状态。
  • Polling controller watches for this state to change. Usually a DB record etc.
  • 轮询控制器监视此状态的变化。通常是DB记录等

The nice bit about using a framework like this is that if your service goes down, or you redeploy it, any processing can queue and resume, so you won't have any downtime.




For long running operations you need separate Windows Service application (or Worker Role, if it is Windows Azure). IIS may kill ASP.NET processes on pool recycling and your operation will not finish.

对于长时间运行的操作,您需要单独的Windows服务应用程序(或者工作者角色,如果它是Windows Azure)。 IIS可能会在池回收时终止ASP.NET进程,您的操作将无法完成。

Message queue is mostly for communication. You can use it between your web and worker parts. But it is not required there unless your data is not super critical. You can establish communication using database, cache, file system or 100 other different ways :)


You can use SignalR to notify your client about finished processing.




© 2014-2019 粤ICP备14056181号