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

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

问题描述

每天一次,我想要可能在多台服务器上运行的ASP.NET MVC4网站向我发送一个报告。这似乎是一个很常见的事情想要做,但是我有一个艰难的时刻提出了一个很好的方式来做到这一点。

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.

试图运行一个计时器在服务器上做这个工作是有问题的,有几个原因。如果我有多个服务器,那么我会在所有的服务器上运行定时器(如果服务器关闭);我需要在他们之间进行协调,这很复杂。此外,尝试通过后台线程通过Entity Framework访问数据库时,会增加复杂性,我必须采用锁定策略来在定期后台线程和前台控制器线程之间对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问题,因为数据库访问发生在正常的Controller操作中,并且与数据库的所有其他Controller访问序列化。但是我仍然坚持可能有一个以上的计时器运行的问题,所以我需要一些聪明的控制器操作来忽略多余的报告请求。

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?

推荐答案

Phil Haack很好地解释了你不应该从你的网络应用程序做这个任务它在 他的博客文章

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 Scheduler定期运行的控制台应用程序执行此任务。

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天全站免登陆