0

I have an HttpModule, something like this:

public class MyModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
         context.AcquireRequestState += Context_OnAcquireRequestState;
    }

    private void Context_OnAcquireRequestState(object sender, EventArgs e)
    {
    HttpContext context = ((HttpApplication)sender).Context;
        ... etc
    }
}

I have noticed a problem occurring directly after the app pool is restarted, in which null is passed as the sender parameter of the AcquireRequestState event and I get a null reference exception. After some time the problem seems to resolve itself.

How could this be happening?

cbp
  • 25,252
  • 29
  • 125
  • 205
  • I'm not so sure i'd be concerned with how it's happening as i would be with just dealing with it, ala if (Sender != null) – Al W Dec 18 '09 at 01:54
  • But I need the HttpModule to run - it is important! – cbp Dec 18 '09 at 12:06

1 Answers1

1

HttpContext.Current should be set on the event handler thread by the infrastructure. Try using the static accessor instead of the sender on the event.

nitzmahone
  • 13,720
  • 2
  • 36
  • 39