Search code examples
.netasp.net-mvciishttpmodule

httpModule does not fire when published to IIS


When I am debugging on my machine httpModule fires normally now after publishing it to IIS it does not run, what am I doing wrong? Am I missing something?

here is what it looks like in web.config

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

then the Scheduler looks like this

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
    }
}
}

Solution

  • If you are hosting on IIS 7.0+ integrated pipeline mode make sure that you have declared your module inside the <system.webServer><modules>...<modules></> section:

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

    And by the way make sure you have read The Dangers of Implementing Recurring Background Tasks in ASP.NET article by Phil Haack before putting this code in production.