在ASP.NET上启动维护过程的最佳实践是什么

在ASP.NET上启动维护过程的最佳实践是什么

What is the Best Practice to Kick-off Maintenance Process on ASP.NET

对于ASP.NET应用程序,我需要定期(每天,每小时等)运行维护过程。

在不依赖于服务器上预定任务之类的外部流程的情况下,实现此目标的最佳方法是什么(假设我无权访问服务器-共享主机环境)。


这是StackOverflow的执行方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private static CacheItemRemovedCallback OnCacheRemove = null;

protected void Application_Start(object sender, EventArgs e)
{
    AddTask("DoStuff", 60);
}

private void AddTask(string name, int seconds)
{
    OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
    HttpRuntime.Cache.Insert(name, seconds, null,
        DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
        CacheItemPriority.NotRemovable, OnCacheRemove);
}

public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
{
    // do stuff here if it matches our taskname, like WebRequest
    // re-add our task so it recurs
    AddTask(k, Convert.ToInt32(v));
}

详细信息:http://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/


尽管Cache解决方案适用于简单情况,但如果您的日程安排需要更改,您将很不走运。相反,您可以使用Quartz.NET,它是流行的Java框架Quartz的端口,它非常灵活。


如果您不需要按计划的时间执行此操作,而只需要"偶尔一次"清理,则一种方法是在Global.asax Session_OnEnd()中创建一个函数,该函数将创建一个随机变量。 1到100之间的数字,如果数字是50,则执行维护任务。

偏离路线时,您可以减少" 100",以使任务更频繁地发生。

在http://www.codeproject.com/aspnet/ASPNETService.asp上也有标题为"使用ASP.NET模拟Windows服务以运行计划的作业"的文章,该文章使用过期缓存来模拟计时器。它声称它可以在任何托管站点上运行。

如果您使用的是最后一种,请从有关此技术的帖子中阅读此评论:

You need to really be careful on the
length of the task running. Every new
Task is a new Worker Thread and
there’s a limited number of those - as
it"borrows" a thread from the managed
thread pool.

Starting in v3.5 of the Framework the
maximum number of threads was
increased 10x from 25 to 250. But
there’s now a logarithmic startup to
them, so as it doles out more threads
it gets stingier with them. If you run
out of available threads in the
managed thread pool - your response
times are going to go through the
roof.

What you’re really writing here is a
messaging/queuing system.

If you’re doing things like updating
the cache, then by all means - kick
off a new task. If you’re doing
something like downloading a secondary
HTTP resource or some kind of
intensive database work - write a
Windows Service and use a Queue that
allows you more control over how much
you"bite" off each time.


尽管StackOverflow做到这一点的方式绝对是唯一的,但由于涉及到这个问题,您可能还希望监视这个问题。


这是一个外部过程,我不知道它的可靠性如何,但是您可以在始终位于www.webcron.org的计算机上进行类似设置。

基本上,它的作用是按您请求的时间表访问您请求的页面。

从本质上讲,您可以按计划在页面上执行任何操作,从而可以启动维护任务。

Jeff和Joel在最近的播客中还讨论了通过其他方法做类似的事情。


推荐阅读

    linux退出启动命令行?

    linux退出启动命令行?,系统,状态,档案,平台,命令,环境,模式,终端,程序,编辑,l

    linux服务端退出命令?

    linux服务端退出命令?,档案,命令,环境,异常,标准,网络,模式,终端,编辑,文件,l

    linux中启动服务命令?

    linux中启动服务命令?,服务,系统,命令,信息,工作,设备,网络,标准,名称,密码,l

    linux服务器常用命令?

    linux服务器常用命令?,工作,系统,地址,信息,命令,目录,管理,标准,设备,功能,

    linux永久启动命令?

    linux永久启动命令?,系统,服务,密码,软件,工具,电脑,位置,环境,状态,发行,lin

    linux命令行启动软件?

    linux命令行启动软件?,系统,工具,软件,位置,密码,电脑,首页,代码,环境,地方,l

    linux筛选服务命令?

    linux筛选服务命令?,服务,系统,状态,软件,环境,主体,技术,号码,发行,名称,查

    linux服务器保存命令?

    linux服务器保存命令?,时间,状态,档案,电脑,命令,信息,位置,编辑,文件,模式,L

    linux服务器扫盘命令?

    linux服务器扫盘命令?,地址,工作,命令,目录,数据,单位,名称,系统,管理,信息,L

    linuxu盘启动命令?

    linuxu盘启动命令?,系统,软件,工具,数据,设备,位置,盘中,电脑,第一,信息,在li

    linux命令切换服务器?

    linux命令切换服务器?,地址,名称,系统,环境,实时,命令,服务器,脚本,路径,版

    linux中启动软件命令?

    linux中启动软件命令?,环境,软件,电脑,系统,工具,位置,代码,设备,时间,情况,

    命令行添加linux启动?

    命令行添加linux启动?,系统,软件,工具,环境,初级,地址,发行,命令,目录,终端,l

    linux增加自启动命令?

    linux增加自启动命令?,服务,系统,信息,工具,软件,查询系统,状态,跨行,情况,

    linux上启动脚本命令?

    linux上启动脚本命令?,服务,状态,系统,代码,脚本,工作,周期性,命令,文件,方

    linux服务器搭建命令?

    linux服务器搭建命令?,系统,服务,软件,地址,平台,在线,密码,工具,环境,百度,l

    服务器重启命令linux?

    服务器重启命令linux?,工作,标准,设备,服务,系统,名称,命令,百度,网络,密码,

    linux重新启动命令是?

    linux重新启动命令是?,系统,工作,设备,标准,命令,灵活,用户,电源,提示,主机,l

    linux服务端常用命令?

    linux服务端常用命令?,工作,地址,系统,网络,基础,命令,标准,工具,信息,管理,l

    linux启动jre命令?

    linux启动jre命令?,系统,软件,代码,环境,平台,网站,企业,中心,通用,做好,如何