System.Web.HttpException happening when session expires or if it is abandoned

Jon Dadi 21 Reputation points
2021-09-16T15:42:23.487+00:00

I have been dealing with this sneaky exception for a while, and I'm having a very hard time figuring it out. When a session expires or when I call Session.Abandon() the application will run for a second and then Visual studio will tell me it is in a break mode and will stop the debugging. When this happens on our production server it will kill the application pool. The project is a .Net webforms application running on .Net 4.5.2.

I've tried stepping through all the code after calling Session.Abandon() and it will throw the exception on random places.

The stack trace:

Exception: System.Web.HttpException
Message: Exception of type 'System.Web.HttpException' was thrown.
StackTrace:    at System.Web.HttpAsyncResult.End()
   at System.Web.Util.AspCompatApplicationStep.RaiseAspCompatEvent(HttpContext context, HttpApplication app, String sessionId, EventHandler eventHandler, Object source, EventArgs eventArgs)
   at System.Web.HttpApplicationFactory.FireSessionOnEnd(HttpSessionState session, Object eventSource, EventArgs eventArgs)
   at System.Web.SessionState.SessionOnEndTargetWorkItem.RaiseOnEndCallback()
   at System.Web.Util.WorkItem.CallCallbackWithAssert(WorkItemCallback callback)
   at System.Web.Util.WorkItem.OnQueueUserWorkItemCompletion(Object state)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
InnerException: System.ArgumentNullException
Message: Value cannot be null.
Parameter name: httpContext
StackTrace:    at System.Web.HttpContextWrapper..ctor(HttpContext httpContext)
   at System.Web.HttpApplication.<>c__DisplayClass284_0.<OnExecuteRequestStep>b__0(Action nextStepAction)
   at System.Web.HttpApplication.StepInvoker.Invoke(Action executionStep)
   at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

All help is greatly appreciated!

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,357 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,249 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,223 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
938 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 47,806 Reputation points
    2021-09-16T15:54:24.97+00:00

    The actual exception that is causing this is a bad argument (null) being passed to HttpContextWrapper. This is causing cascading failures. The question becomes why would it do that. At the top of my list of suspects is any code that you have written (perhaps in Application) that is using HttpContext. Perhaps it is on your EndRequest or Session_End methods or similar. It could also be caused by a handler you rely on.

    To debug this I would change the exception settings in the debugger to break when ArgumentNullException is thrown. Then run the code and let it fail. When the exception is thrown you'll get thrown into the debugger (perhaps even before your test case occurs). Once in the debugger however you have the callstack and can hopefully find some code either written by you or a third party you rely on that is causing the problem.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful