Mark Gilbert's Blog

Science and technology, served light and fluffy.

No RSVP required: MvcApplication_BeginRequest and MvcApplication_EndRequest

A few weeks ago I started a new project, and I needed to do some work before and after every request.  It’s a Web API project, so the methods I needed to tap into were MvcApplication_BeginRequest and MvcApplication_EndRequest.

Sounds simple, and usually it would be.  Except that this will be the first project I do in 100% C#.  While I’ve been a polyglot developer for the last few years, I’ve only used C# for the test suites.  VB has been my "goto" language for the actual application (I love language humor).  And while I’ve gotten pretty good at writing in C# and converting between C# and VB, there are still nooks and crannies to C# that cause me to stumble.  How to get ahold of these two event handlers became one of them.

I began with Application_Start.  I figured that would be the place to register the event handlers:

protected void Application_Start()
{
    this.BeginRequest += MvcApplication_BeginRequest;
    this.EndRequest += MvcApplication_EndRequest;

    // More start-up stuff here
}

protected void MvcApplication_BeginRequest(object sender, EventArgs e)
{
    // Do stuff
}

protected void MvcApplication_EndRequest(object sender, EventArgs e)
{
    // Do stuff
}

I hit F5, and was greeted with this error:

10

[NullReferenceException: Object reference not set to an instance of an object.]
    System.Web.PipelineModuleStepContainer.GetStepArray(RequestNotification notification, Boolean isPostEvent) + 25
    System.Web.PipelineStepManager.ResumeSteps(Exception error) +984
    System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb) +95
    System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +186

Well that’s a new one.  Consulting the internets turned up a couple of posts that showed me where I had gone astray:

http://stackoverflow.com/questions/6590578/how-do-you-wire-up-application-beginrequest-in-asp-net-mvc

http://www.west-wind.com/weblog/posts/2009/Jun/18/How-do-ASPNET-Application-Events-Work

Basically, explicitly adding the event handlers was unnecessary – in fact, it was downright counterproductive.  ASP.NET will find these handlers through Reflection – I didn’t need to wire them up at all.

Incidentally, when I removed the explicit wire-up for this.BeginRequest, but kept the one for this.EndRequest, the error message changed slightly:

20

[NullReferenceException: Object reference not set to an instance of an object.]
    System.Web.PipelineStepManager.ResumeSteps(Exception error) +224
    System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb) +95
    System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +186

Not the clearest exception messages on the planet, but thankfully this issue only set me back a few minutes.  Onward!

About these ads

June 28, 2013 - Posted by | ASP.NET MVC, Web API

Sorry, the comment form is closed at this time.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: