ASP.NET MVC重复任务 [英] ASP.NET MVC Repeating Task

查看:152
本文介绍了ASP.NET MVC重复任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一个ASP.NET MVC的网站多次运行的任务;它需要每一个运行的的时间间隔(间隔无所谓)

我通过设置在Global.asax.cs中的Application_Start一个后台线程它运行的任务,休眠做到了这一点的的时间间隔的,然后再次运行....又一遍。

虽然这个作品它似乎不喜欢这样做的正确的方式,似乎有点哈克。

于是我用一个Timer对象替换它,这并不可靠地工作 - 别人看来有它的问题,以及 - 所以我不打算使用这种

这需要在MVC网站上运行,它是用于从队列拉动就业,并从视图渲染他们准备好发送电子邮件。

我想过使用线程池,并获得previous工作离开高就它的方式出来,但因为这些可以长期运行的工作我读过,这可能最终没有留下足够的线程来处理网络通过饥饿的请求。

在与原来的线程方法坚持我想我会问,如果任何人都知道这样做的更好的方法。

我希望这是有道理的。有效地我想要实现的是心跳。一些充当生产者/消费者模式在MVC网站消费的一部分。


解决方案

#1本身使用(或至少习惯使用)这样做的巧妙方法。在这里看到: http://blog.stackoverflow.com/2008 / 07 /易后台任务 - 在ASPNET /

在简单地说,你这样做:


  

      
  1. 在启动时,添加项目到 HttpRuntime.Cache 以固定
      过期。


  2.   
  3. 在缓存项过期,做你的工作,如WebRequest的或有什么
      您。


  4.   
  5. 有固定到期的项目重新添加到缓存中。


  6.   

I need to run a task repeatedly on an ASP.NET MVC site; it needs to run every time interval (Interval doesn't matter).

I achieved this by setting a background thread from the Global.asax.cs Application_Start which runs the task, sleeps for time interval and then runs again....and again.

While this works it doesn't seem like the right way to do this, seems a bit hacky.

So then I replaced it with a Timer object, this doesn't work reliably - seems others have had problems with it as well - so I'm not going to use this.

This needs to run in the MVC site, it is used for pulling jobs from a queue and rendering them from the Views ready for emailing.

I thought about using the ThreadPool and getting the previous job to leave another job on it's way out but because these can be long running jobs I've read that this can end up not leaving enough threads to deal with web requests through starvation.

Before sticking with the original thread method I thought I'd ask if anyone else knows a better way of doing this.

I hope this makes sense. Effectively what I'm trying to achieve is a heartbeat. Something to act as the consumer part of the producer/consumer pattern in an MVC site.

解决方案

Stackoverflow itself uses (or at least used to use) a cunning way of doing this. See here: http://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/

In brief, you do this:

  1. At startup, add an item to the HttpRuntime.Cache with a fixed expiration.

  2. When cache item expires, do your work, such as WebRequest or what have you.

  3. Re-add the item to the cache with a fixed expiration.

这篇关于ASP.NET MVC重复任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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