让 asp.net 自动触发事件和方法的最佳方式 - 根据时间表 -
嗯,我相信这不是第一次看到这个问题。老实说,您可以在许多其他网站和博客上找到这个问题的答案 - 不仅在这里 -
但我从未见过这个问题的完整答案,我的意思是没有人谈论优点和缺点。没有人分享真实的经历。
虽然 Quartz.NET 似乎是一个很好的解决方案,但是,还没有足够的评论或不同方式之间的良好比较才能实现这样的事情。
据我了解,我发现以下是执行此操作的可能方法:
使用:Web 服务、Windows 应用程序、控制台应用程序、Quartz.NET。
我想要使用计划或自动方法/事件实现的示例,例如在一段时间后归档,自动删除/移动数据库中的记录,在一定时间后设置对象的属性,根据我的自动运行一些方法假期安排。
我希望您能分享其中一种方法的经验或分享一种新的方法。
Well, I'm sure this is not the first time to see this question. and TBH you can find this question answered on many other websites and blogs -not only here-
But I never saw a complete answer for this question, I mean no one talked about the advantages and the disadvantages. no one shared a real experience.
Although Quartz.NET seems like a good solution but yet, there's no enough reviews or a good comparison between the different ways to achieve such a thing.
As far as I read about it I found this are the possible ways of doing this :
Using a: Web Service, Windows Appliction, Console Application, Quartz.NET.
Examples of stuff I want to achieve with the scheduled or automatic methods/events are like archiving after a certain period, auto deleting/moving records in the database, setting a property of an object after a certain time, automatically run some methods according to my holiday schedule.
I hope you'd share your experience either with one of those approaches or share a new way of doing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我工作的电子商务网站中,我们在名为“Worker”的 Windows 服务中使用 Quartz.NET。它通过以编程方式定义的计划执行 Quartz.NET
IJob
实现,并独立于网站进程运行。它的作用包括批量发送电子邮件、更新统计信息、刷新 SOLR 索引等。这样,我们就不必担心由于工作进程回收而导致我们的日程安排被重置或错过,也不必担心由于网络花园而在同一台机器上同时运行多个实例的疯狂情况。 Worker 的安装、启动和停止独立于网站,甚至可以存在于单独的计算机上,从而提供维护和部署的灵活性。
Web 站点和 Worker 都共享一个公共 Model DLL,其中包含对象模型和 NHibernate 配置,因此业务逻辑没有太多逻辑重复。
在创建 Worker/Windows 服务之前,我依赖于通过计划任务执行的简单控制台应用程序。在某种程度上,这种方法效果很好,但随着站点变得越来越复杂,需要定期运行越来越多的批处理作业,我发现自己维护了很多独立的控制台程序和很多独立的计划任务,随着所需职位数量增加。通过将 Quartz.NET 的调度整合到单个 Windows 服务中,我现在只需关注一个进程,并且可以确保任何业务逻辑更改都是“同步”的,因为模型只需在两个位置进行更新 - - Worker 和网站(而不是许多)网站和许多小型控制台应用程序。
我探索了一些选项,例如将运行时存储在 ASP.NET 缓存中或在工作进程的内存中保留引用,但我发现 (1) 这使得任务的确切执行时间取决于对网站的传入请求,并且 (2)使用网络花园会导致不必要的重复工作(因为任务将被“安排”在同一台机器上的多个工作进程中)。
在我看来,从长远来看,创建一个可以通过常用 Windows 机制启动和停止的 Windows 服务已被证明是最合乎逻辑、直观且可维护的结果。
网站和 Worker 都使用 WiX 编译为两个 Windows Installer MSI 文件,因此更新它们非常简单 - 只需双击服务器上的 MSI,我们的更新就可以开始了。
希望有帮助!
At the e-commerce Web site that I work for, we use Quartz.NET in a Windows service called the "Worker". It executes Quartz.NET
IJob
implementations via the programmatically-defined schedule and runs independently of the Web site processes. It does things like dispatch e-mails in batches, update statistics, refresh the SOLR indexes, and so on.This way, we don't have to worry about our schedules getting reset or missed as a result of worker processes recycling, or dealing with the madness of having multiple instances running simultaneously on the same machine as a result of a Web garden. The Worker is installed, started, and stopped independently of the Web site, and could even exist on a separate machine, giving flexibility in maintenance and deployment.
The Web site and the Worker both share a common Model DLL that contains the object model and NHibernate configuration, so there is not much logical duplication of business logic.
Before I created the Worker/Windows service, I relied on simple console applications that were executed via scheduled tasks. This worked well up to a point, but as the site became more complex and required more and more batch jobs to run periodically, I found myself maintaining a lot of independent console programs and a lot of independent scheduled tasks, which became more difficult as the number of jobs required increased. By consolidating scheduling with Quartz.NET into a single Windows service, I now only have one process to keep an eye on and can be assured that any business logic changes are "in sync" since the model only has to be updated in two places--the Worker and the Web site--instead of many--the Web site and lots of little console applications.
I explored options like storing runtimes in the ASP.NET cache or keeping references around in the worker process's memory, but I found that (1) this made the exact execution time of the task dependent on incoming requests to the Web site and (2) the use of a Web garden caused unnecessary duplication of effort (since the task would be "scheduled" in multiple worker processes on the same machine).
Creating a Windows service that can be started and stopped via the usual Windows mechanisms has proved to be, in my humble opinion, the most logical, intuitive, and maintainable result in the long run.
Both the Web site and Worker use WiX to compile down to two Windows Installer MSI files, so updating them is a snap--just double-click the MSI on the server, and our update is good to go.
Hope that helps!
明显的问题是 Web 应用程序旨在作为请求-响应服务。因此,尽管可能是可破解的(当然人们也有),但您需要响应请求才能启动某种处理任务。我知道您想要一个 Web 表单解决方案,但我建议这种类型的问题域不能通过 Web 平台得到最好的解决。
作为一个建议,虽然您可能不想听到这个建议,但您当然可以使用 Windows 服务解决方案以及该平台附带的所有好东西(后台计时器、MSMQ 处理、线程、更轻松的安全集成等)并显示它的效果通过基于 Web 的 UI 应用程序(通过 WCF 连接)获取信息。
The obvious problem will be that a web application is intended as a request-response service. So, although probably hackable (and surely people have), you would need to respond to a request in order to kick off some sort of processing task. I know you want a webforms solution, but I would suggest this type of problem domain is not best solved with a web platform.
As a suggestion, although maybe not one you wanted to hear, you could of course use a Windows Service solution and all the good things that go with that platform (background timers, MSMQ processing, threading, easier security integration, etc) and surface it's information via a web based UI application (connected via WCF).
查看以下提出类似问题的问题:
https://stackoverflow.com/questions/3243400/how-to-do-background-processing-similar-to-that-on-stackoverflow/
我链接到 杰夫·阿特伍德本人有一种独特的方法来实现这一目标:
https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/
我不认可这种方法,但这是一个很好的方法如果您不想提供服务或使用其他工具,则可以使用替代方案。
Check out the following question that was asking something similar:
https://stackoverflow.com/questions/3243400/how-to-do-background-processing-similar-to-that-on-stackoverflow/
I linked to a blog post by Jeff Atwood himself that had a unique approach to accomplish this:
https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/
I am not endorsing this approach but it's a good alternative if you don't want to make a service or use another tool.
我见过至少有 100 种方式提出这个问题。根据我的经验,进行调度的最佳方法是从深思熟虑的 n 层设计开始。这使您可以根据需要灵活地更改前端。例如,控制台应用程序调用由 Windows 任务调度程序处理的 Method1()。我强烈建议您坚持使用 Windows 任务计划程序,除非要求迫使您这样做。
从概念上讲,计时器是一个好主意。在执行过程中,它们可能会出现错误,或者根本无法运行,具体取决于环境(某些计时器在 win 服务中不起作用)。
就 Windows 应用程序而言,我从未将其用于计划任务(除了将控制台应用程序编译为 winform 应用程序之外,因此控制台窗口不会显示)。因为它显示不必要的 UI,使用户感到困惑。
在数据库方面,您有新的选项,例如作业和触发器。
总而言之,没有灵丹妙药。我建议您默认使用控制台应用程序并重构您的代码,以便您有一个漂亮的设计。如果需要,请阅读设计模式书籍。
I've seen this question asked in at least a 100 ways. In my experience the best way to go about scheduling is to start with a well thought out n-tier design. This allows you the flexibility to change out front ends as needed. For example, a console application to call Method1() which is handled by the windows task scheduler. I firmly suggest you stick to the windows task scheduler unless requirements drive you to do otherwise.
Timer's are a great idea, in concept. In execution they can be buggy or simply not function whatsoever depending on the environment (some timers don't work in win services).
As far as a Windows Application, I have never used this for a scheduled task (outside of compiling a console app as a winform app so the console window won't display). As it displays unecessary UI, confusing the user.
On the DB side you have new options like jobs and Triggers.
In summary, there is no silver bullet. I suggest you default to a console app and refactor your code so you have a nice design. Read a design pattern book if you need to.