0

当我在我的机器上调试时,httpModule 在将它发布到 IIS 后现在正常触发,它没有运行,我做错了什么?我错过了什么吗?

这是 web.config 中的样子

<httpModules>
     <add type="MyApp.Web.Mvc.Modules.Scheduler" name="Scheduler" />
</httpModules>

那么调度程序看起来像这样

public class Scheduler : IHttpModule
{   
private static readonly object Job;
private static Timer timer;

static Scheduler()
{
    Job = new object();
}

void IHttpModule.Init(HttpApplication application)
{
    try
    {

        if (timer == null)
        {
            var timerCallback = new TimerCallback(ProcessJobs);
            const int startTime = 10 * 1000;
            const int timerInterval = 60 * 1000; // 1 minute
            timer = new Timer(timerCallback, null, startTime, timerInterval);
        }
    }
    catch (Exception ex)
    {
        //exception code here
    } 
}

public void Dispose()
{
}

protected void ProcessJobs(object state)
{
    try
    {
        // This protects everything inside from other threads that might be invoking this
        // which is good for long running processes on the background
        lock (Job)
        {
            //My Stuff
        }
    }
    catch (Exception ex)
    {
        //exception code here
    }
}
}
4

1 回答 1

3

如果您在 IIS 7.0+ 集成管道模式上托管,请确保您已在该<system.webServer><modules>...<modules></>部分中声明了您的模块:

<system.webServer>
    ....
    <modules>
        <add name="Scheduler" type="MyApp.Web.Mvc.Modules.Scheduler" />
    </modules>
</system.webServer>

顺便说一句,在将此代码投入生产之前,请确保您已阅读Phil Haack 撰写的在 ASP.NET 中实现重复后台任务的危险一文。

于 2012-10-17T21:09:06.000 回答