当我在我的机器上进行调试时,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
}
}
}
答案 0 :(得分: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撰写的The Dangers of Implementing Recurring Background Tasks in ASP.NET文章。