gpt4 book ai didi

asp.net - Global_asax_BeginRequest - 此行如何出错?

转载 作者:行者123 更新时间:2023-12-02 15:19:00 25 4
gpt4 key购买 nike

我在 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
...

错误是...

ERROR - Global_asax:System.NullReferenceException: Object reference not set to an instance of an object.

我对这个特定的行如何出错感到有点困惑。我想做的就是测试该对象是否为 null/Nothing。

我猜当请求开始时,幕后一定发生了其他事情,但我不知道如何进一步调试它。

此错误并非每次都会发生。我只是偶尔在日志中看到这些错误,但我不知道它们是如何发生的。我无法重现它。由于无法访问 Request 对象,我无法获得有关导致该对象的请求类型的任何其他信息。

更新...

我尝试更改访问 Request 属性的方式,看看是否会产生任何影响...

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
...

这次,有趣的是,异常发生在这一行...

Dim app As HttpApplication = TryCast(sender, HttpApplication)

这看起来很奇怪,因为 TryCast 是专门为了不抛出异常而设计的。

这是我得到的完整堆栈跟踪...

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)

第 97 行对应于 TryCast 行。

我目前的理论是,可能与 Owin 中间件有关

最佳答案

所描述的情况似乎不可能(我稍后会解释),例如我会推荐更多的防弹诊断

Try
Dim locReq As HttpRequest = Request
Catch ex As Exception
'record Exception with the complete stack trace
Throw
End Try

并使用 HttpRequest 变量 (locReq) 而不是 Request 属性。该代码片段还可以防止更改 Request 属性返回值。

为什么我认为你的假设不可能:

无法在该行中获取 NullReference

If Request IsNot Nothing ...

但它可以来自 Request 属性的深度。它(翻译)source code

Public Class HttpApplication
'...
Public ReadOnly Property Request As HttpRequest
Get
Dim request As HttpRequest = Nothing
If _context IsNot Nothing AndAlso
Not _hideRequestResponse Then request = _context.Request

If request Is Nothing Then Throw New HttpException(SR.GetString(SR.Request_not_available))
Return request
End Get
End Property

这里又是一个属性请求(已翻译)source code

Public NotInheritable Class HttpContext
'...
Private _response As HttpResponse
'...
Public ReadOnly Property Response As HttpResponse
Get
If HideRequestResponse OrElse
HasWebSocketRequestTransitionCompleted Then Throw New
HttpException(SR.GetString(SR.Response_not_available))
Return _response
End Get
End Property

当我阅读代码时,我发现没有空引用的空间,但可能存在 HttpException。所以我的结论是日志系统错误地记录了该行或异常。

请查看IIS7 Integrated mode: Request is not available in this context exception in Application_Start这与 Response_not_available 异常有关。

编辑

我试图解释所呈现的堆栈跟踪。我从最深的过程中得出的结论(ExecuteStep(IExecutionStep step, ref bool completedSynchronously)是异常只能发生在它的catch block 中(用原始注释翻译)。

Catch e As Exception
[error] = e
'Since we will leave the context later, we need to remember if we are impersonating
'before we lose that info - VSWhidbey 494476

If ImpersonationContext.CurrentThreadTokenExists Then
e.Data(System.Web.Management.WebThreadInformation.IsImpersonatingKey) = String.Empty
End If
'This might force ThreadAbortException to be thrown
'automatically, because we consumed an exception that was
'hiding ThreadAbortException behind it

If TypeOf e Is ThreadAbortException AndAlso ((Thread.CurrentThread.ThreadState And ThreadState.AbortRequested) = 0) Then
[error] = Nothing
'Response.End from a COM+ component that re-throws ThreadAbortException
'It is not a real ThreadAbort
'VSWhidbey 178556
_stepManager.CompleteRequest()
End If
End Try

由此看来,问题确实与中止线程有关。我的推测是 _stepManager 没什么,但我不知道为什么。

关于asp.net - Global_asax_BeginRequest - 此行如何出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53183483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com