简体   繁体   English

如何在C#MVC Web应用程序中发生定期事件?

[英]How to have periodical events in a C# MVC web app?

I have a web app that I'm writing right now that is supposed to have "periodical events". 我有一个现在正在编写的Web应用程序,应该有“定期事件”。 For instance, at midnight, the web app should calculate "scores" for all users. 例如,在午夜,Web应用程序应为所有用户计算“分数”。 I want this done only once during the day. 我只希望白天完成一次。 Is there a way that I can automate this, so it runs automatically at midnight (or whatever hour I choose)? 有没有一种方法可以使它自动化,以便它在午夜(或我选择的任何时间)自动运行?

I don't like the idea of creating a separate script (VBS) to do this, as the calculation would depend on a lot of business logic of the app. 我不喜欢创建单独的脚本(VBS)来执行此操作的想法,因为计算将取决于应用程序的许多业务逻辑。 I was thinking to put it into a separate Class Library, so it can use the web app logic (which is also in a class library), but is this the best way to go about it? 我当时正在考虑将其放入一个单独的类库中,以便可以使用Web应用程序逻辑(也在类库中),但这是实现它的最佳方法吗? I also don't like the idea of using the Session_Start() event in the Global.asax to trigger the event by checking the hour manually. 我也不喜欢在Global.asax中使用Session_Start()事件通过手动检查小时数来触发事件的想法。 There must be some easier way - especially because down the road I expect there will be a lot more of periodical events - some may have to be triggered every fifteen minutes, for example ... 必须有一些更简单的方法-尤其是因为我希望在以后的工作中会有更多的定期事件-例如,某些事件可能必须每隔15分钟触发一次。

Thanks a lot for any help you can give me. 非常感谢您能给我的任何帮助。

You should not do this in the web app itself. 您不应该在网络应用程序本身中执行此操作。 You are correct to put the business logic in a separate library. 您将业务逻辑放在单独的库中是正确的。 Once you have done this, you can use the business logic from anywhere, and therefore, a good solution would be to create a console application that does the nightly jobs, and invoke the console application from Windows Task Scheduler. 完成此操作后,您可以在任何地方使用业务逻辑,因此,一个好的解决方案是创建一个执行夜间工作的控制台应用程序,然后从Windows Task Scheduler调用该控制台应用程序。 IIS is not suitable as a host for periodical events. IIS不适合作为定期事件的宿主。

Yeah... again awesome "change your architecture and hosting environment so that my answer can be relevant" responses. 是的。。。很棒的“更改您的体系结构和托管环境,以便我的答案可以是有意义的”响应。

Doing what you ask is actually quite easy, take a look at this article: http://www.codeproject.com/Articles/12117/Simulate-a-Windows-Service-using-ASP-NET-to-run-sc 做您想做的事实际上很容易,请看一下这篇文章: http : //www.codeproject.com/Articles/12117/Simulate-a-Windows-Service-using-ASP-NET-to-run-sc

I guess you are missing the point of separation of concern. 我想您错过了关注点分离问题。 Whatever you are asking is a job of a service . 您要问的是服务工作 You need to develop a separate application as Windows Service that will do all your calculation and to be triggered by any scheduler even Windows Task Scheduler would do. 您需要开发一个单独的应用程序作为Windows服务 ,该应用程序将执行所有计算,并且即使Windows Task Scheduler也会由任何调度程序触发。 This is what basically done on large scale applications. 这基本上是在大型应用程序上完成的。

This is a job for a windows service or scheduled task. 这是Windows服务或计划任务的工作。 A web application responds to HTTP requests. Web应用程序响应HTTP请求。 Essentially the service's job would be to wake up, run the appropriate calculations and write back to the database. 本质上,服务的工作是唤醒,运行适当的计算并写回数据库。 Once in the database, your web application can use the newly calculated values. 一旦进入数据库,您的Web应用程序就可以使用新计算的值。

Here is some information on windows services: http://msdn.microsoft.com/en-us/library/d56de412.aspx 这是有关Windows服务的一些信息: http : //msdn.microsoft.com/zh-cn/library/d56de412.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM