I have the following defined in my Global.asax.vb...
Private Sub Global_asax_BeginRequest(sender As Object, e As EventArgs) Handles Me.BeginRequest
Try
If Request IsNot Nothing Then 'this line throws an exception...
With Request
...
The error is ...
ERROR - Global_asax:System.NullReferenceException: Object reference not set to an instance of an object.
I'm a bit confused as to how this particular line can error. All I'm trying to do is test to see if the object is null/Nothing.
I'm guessing there must be something else happening behind the scenes when the request begins, but I don't know how to debug it further.
This error doesn't occur every time. I'm just seeing these errors occasionally in the logs, and I don't know how they're occurring. I'm not able to reproduce it. Being unable to access the Request object, I can't get any other information about the type of request that causes it.
update...
I tried changing the way I access the Request property, to see if it would make any difference...
Public Sub Application_BeginRequest(sender As Object, e As EventArgs)
Dim app As HttpApplication = TryCast(sender, HttpApplication)
If app IsNot Nothing Then
Dim _request = app.Request
...
This time, interestingly, the exception occurred at this line...
Dim app As HttpApplication = TryCast(sender, HttpApplication)
This seems very strange, as TryCast is specifically intended to not throw exceptions.
This is the full stack-trace I'm getting...
System.NullReferenceException: Object reference not set to an instance of an object.
at Global_asax.Application_BeginRequest(Object sender, EventArgs e) in C:\vs_agent\_work\4\s\...\Global.asax.vb:line 97
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Line 97 corresponds to the TryCast line.
My current theory is that perhaps it has something to do with the Owin middleware