如何在ASP.NET MVC网站上定期进行工作的? [英] How to perform periodic work on an ASP.NET MVC website?

查看:103
本文介绍了如何在ASP.NET MVC网站上定期进行工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每天一次,我希望我的ASP.NET MVC4网站,该网站可以在多台服务器上运行,将报表通过电子邮件发送给我。这似乎是一个pretty常见的事想做,但我有一个艰难的时间想出一个好办法做到这一点。

Once a day, I want my ASP.NET MVC4 website, which may be running on multiple servers, to email a report to me. This seems like a pretty common thing to want to do, but I'm having a tough time coming up with a good way to do it.

试图在服务器上运行一个计时器做这项工作是有两个原因问题。如果我有多个服务器,那么我对所有的人都跑定时器(如果一台服务器宕机);我需要在它们之间进行协调,这变得复杂。此外,试图通过实体框架从后台线程访问数据库补充说,我必须采用锁定策略序列化建设/处置周期性后台线程和前景控制器线程之间的DbContext对象的复杂性。

Trying to run a timer on a server to do this work is problematic for a couple of reasons. If I have multiple servers then I'd have the timer running on all of them (in case a server goes down); I'd need to coordinate between them, which gets complicated. Also, trying to access the database via Entity Framework from a background thread adds the complication that I must employ a locking strategy to serialize construction/disposal of the DbContext object between the periodic background thread and the "foreground" Controller thread.

另一种选择是运行在服务器上的计时器,但有计时器线程执行GET以魔术页面生成和电子邮件的报告。因为数据库访问发生在一个正常的控制器动作这解决了的DbContext问题,系列化与所有其他控制器访问数据库。但我还是坚持了有可能不止一个定时器运行的问题,所以我需要在控制器动作的一些智慧忽略冗余报告请求。

Another option would be to run a timer on the servers, but to have the timer thread perform a GET to a magic page that generates and emails the report. This solves the DbContext problem because the database accesses happen in a normal Controller action, serialized with all of the other Controller accesses to the database. But I'm still stuck with the problem of having potentially more than one timer running, so I'd need some smarts in the Controller action to ignore redundant report requests.

有什么建议?如何这样的事情通常做什么?

Any suggestions? How is this sort of thing normally done?

推荐答案

您不应该做的Web应用程序此任务菲尔哈克很好地解释了它在<一个href=\"http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx\"><$c$c>his博客文章 。

You should not be doing this task from your web application as Phil Haack nicely explains it in his blog post.

如何这样的事情正常执行?

How is this sort of thing normally done?

您可以从定于使用Windows计划定期运行Windows服务,甚至是控制台应用程序执行此任务。

You could perform this task from a Windows Service or even a console application that is scheduled to run at regular intervals using the Windows Scheduler.

这篇关于如何在ASP.NET MVC网站上定期进行工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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