IIS Express with VS2012 - Null ref when attaching handler to HttpApplication.BeginRequest - by LukePuplett

Status : 

 


5
0
Sign in
to vote
ID 788481 Comments
Status Active Workarounds
Type Bug Repros 1
Opened 5/23/2013 4:06:58 AM
Access Restriction Public

Description

This problem has been floating around for years, in various guises.

http://stackoverflow.com/questions/7005646/nullreferenceexception-pipelinestepmanager-resumesteps-at-godaddy

http://www.bing.com/search?q=%22Object+reference+not+set+to+an+instance+of+an+object%22+BeginRequest+ResumeSteps+BeginProcessRequestNotification&go=&qs=n&form=QBRE&pq=%22object+reference+not+set+to+an+instance+of+an+object%22+beginrequest+resumesteps+beginprocessrequestnotification&sc=0-0&sp=-1&sk=

Essentially, you can't hook-up observers to the BeginRequest, and possibly other events, during Application_Start

From looking at other people's forum posts, the problem seems to be in IIS Express and maybe IIS 7, but possibly not IIS 6.
Sign in to post a comment.
Posted by Micah71381 on 6/19/2013 at 11:05 AM
While the examples provided are the easiest and most common way to reproduce this bug, the bug is in the .NET Framework. I have spent a lot of time digging into this using .NET Reflector to track down the root cause of the bug inside the .NET Framework and as far as I can tell, the HttpApplication gets into a bad state when the same set of events are not hooked up across multiple HttpApplications.

If you have an HttpApplication up and running with a given set of event handlers (some of which come from HttpModules, some come from attaching event handlers in code) and then a second HttpApplication spins up in the same application domain, the second HttpApplication will get into a bad state unless you provide the same set of event handlers. In the examples provided, the same set of events don't get hooked up because the event handlers are only hooked up on the first HttpApplication, they are not reconnected on subsequent HttpApplications. While this is probably not what the programmer intended, the expected behavior is that your event handlers stop working, not that you get a NullReferenceException.

The invalid state *appears* to be in the PipelineModuleStepContainer member in HttpApplication. In my testing, the last HttpModule doesn't get hooked up correctly and this results in a null reference exception as described above. In particular, GetEventCount is called on a PipelineModuleStepContainer that has not had any calls to AddEvent made to it yet.

For the developer that ends up working on this: PipelineModuleStepContainer will always throw a NullReferenceException if it is used before calling AddEvent on it at least once. In the case of this crash, I believe that is what is happening.
Posted by LukePuplett on 6/18/2013 at 1:56 PM
Okay that's fine, but the API therefore needs to give an InvalidOperationException with a message explaining why the operation is indeed invalid. A NullReferenceException shouldn't ever be encountered from a public API.

It may be that other factors/constraints/design decisions have led to this situation, but if there's a way to trap the API misuse and throw a more appropriate exception, then that should be the goal, and it would silence people like me and others hitting this issue, assuming its a bug and filling forums.
Posted by Microsoft on 6/17/2013 at 5:27 PM
The root cause of this issue is a bug in the provided sample code. Pipeline events (like BeginRequest, EndRequest, and others) need to be hooked from within the HttpApplication instance's Init method, not its Application_Start method. Think of HttpApplication instances as special cases of IHttpModule instances: the ASP.NET runtime creates them as necessary and pools them for future use. Since each instance needs to hook the BeginRequest event, the proper place to do this is from within the Init method. See http://msdn.microsoft.com/en-us/library/ms178473%28v=vs.100%29 for more information.
Posted by Microsoft on 6/11/2013 at 1:16 PM
Thanks for your patience. I am able to repro this issue.
Posted by LukePuplett on 6/10/2013 at 6:51 AM
Make a new MVC 4.0 web app in VS2012. In Application_Start add a handler to this.BeginRequest, no code needed in the handler method, then run it. That's all you need to do.
Posted by LukePuplett on 6/10/2013 at 6:50 AM
Grrr. Just wrote a big comment but my session had timed out.
Posted by Microsoft on 6/5/2013 at 8:13 PM
Hi, We are unable to repro this issue. Does it consistently reproduce for you?
Posted by LukePuplett on 5/29/2013 at 3:20 AM
By the way, how can I/we/people get access to post bugs on other products?
Posted by Microsoft on 5/24/2013 at 12:10 AM
Thank you for submitting feedback on Visual Studio and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by Microsoft on 5/23/2013 at 4:50 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)