今天,我们已经构建了一个控制台应用程序,用于运行我们的ASP计划任务。网的网站。但是我认为这种方法容易出错,而且很难维护。如何执行计划任务(在windows/IIS/ASP。网络环境)

更新:

任务示例:

从数据库中的电子邮件队列发送电子邮件 从数据库中删除过时的对象 从谷歌AdWords检索统计数据,并在数据库中填充一个表。


当前回答

All of my tasks (which need to be scheduled) for a website are kept within the website and called from a special page. I then wrote a simple Windows service which calls this page every so often. Once the page runs it returns a value. If I know there is more work to be done, I run the page again, right away, otherwise I run it in a little while. This has worked really well for me and keeps all my task logic with the web code. Before writing the simple Windows service, I used Windows scheduler to call the page every x minutes.

另一种方便的运行方法是使用像Pingdom这样的监视服务。将他们的http检查指向运行您的服务代码的页面。让页面返回结果,然后可以使用该结果触发Pingdom,在出现问题时发送警报消息。

其他回答

一种选择是设置一个windows服务,让它调用你的计划任务。

在winforms中,我使用了计时器,不认为这将在ASP中很好地工作。网

All of my tasks (which need to be scheduled) for a website are kept within the website and called from a special page. I then wrote a simple Windows service which calls this page every so often. Once the page runs it returns a value. If I know there is more work to be done, I run the page again, right away, otherwise I run it in a little while. This has worked really well for me and keeps all my task logic with the web code. Before writing the simple Windows service, I used Windows scheduler to call the page every x minutes.

另一种方便的运行方法是使用像Pingdom这样的监视服务。将他们的http检查指向运行您的服务代码的页面。让页面返回结果,然后可以使用该结果触发Pingdom,在出现问题时发送警报消息。

为什么要重新发明轮子,使用Threading和Timer类。

    protected void Application_Start()
    {
        Thread thread = new Thread(new ThreadStart(ThreadFunc));
        thread.IsBackground = true;
        thread.Name = "ThreadFunc";
        thread.Start();
    }

    protected void ThreadFunc()
    {
        System.Timers.Timer t = new System.Timers.Timer();
        t.Elapsed += new System.Timers.ElapsedEventHandler(TimerWorker);
        t.Interval = 10000;
        t.Enabled = true;
        t.AutoReset = true;
        t.Start();
    }

    protected void TimerWorker(object sender, System.Timers.ElapsedEventArgs e)
    {
        //work args
    }

创建一个自定义的Windows服务。

我把一些关键任务设置为预定的控制台应用程序,发现它们很难维护。我创建了一个带有“心跳”的Windows服务,它每隔几分钟就会检查数据库中的日程安排。结果很好。

话虽如此,我仍然在使用预定的控制台应用程序来完成大部分非关键的维护任务。如果没坏,就别修。

我发现这对所有参与者来说都很简单:

创建一个webservice方法,如DoSuchAndSuchProcess 创建一个控制台应用程序来调用这个webmethod。 在任务调度程序中调度控制台应用程序。

使用这种方法,所有的业务逻辑都包含在你的web应用程序中,但你有windows任务管理器的可靠性,或任何其他商业任务管理器来启动它并记录任何返回信息,如执行报告。使用web服务而不是发布到页面有一点优势,因为它更容易从web服务获取返回数据。