WebAPI HttpContext.Current.Request.Cookies NULL,尽管进行了检查



在过去的一年里,我一直在开发一个应用程序,该应用程序调用API没有问题。

由于部署时,我经常会发现错误日志中最特殊的错误,即没有将非常有用的对象引用设置为对象的实例。

Time: 08:22
Type: System.NullReferenceException
Error: Object reference not set to an instance of an object.
Method: PostClocking
File: SS_PostClocking
Stack: SS_PostClocking.vb:line 71
at SS_PostClocking.vb:line 17

检查线路这条线路上唯一的代码是Dim c = HttpContext.Current.Request.Cookies

我下载了生产数据库,但完全无法复制这个问题,因为我的团队中没有其他人使用任何浏览器。

因此,在过去的几天里,我在上面直接添加了以下内容,试图看看到底发生了什么

Try
If HttpContext.Current Is Nothing Then
ErrorLogging.WriteLog("[Self service post clocking] HttpContext.Current is nothing")
Throw New ManagedException("This action failed, please refresh and try again.")
End If

If HttpContext.Current.Request Is Nothing Then
ErrorLogging.WriteLog("[Self service post clocking] HttpContext.Current.Request is nothing")
Throw New ManagedException("This action failed, please refresh and try again.")
End If
If HttpContext.Current.Request.Cookies Is Nothing Then
ErrorLogging.WriteLog("[Self service post clocking] HttpContext.Current.Request.Cookies is nothing")
Throw New ManagedException("This action failed, please refresh and try again.")
End If
Catch ex As Exception
ErrorLogging.WriteLog(ex)
If ex.InnerException IsNot Nothing Then ErrorLogging.WriteLog(ex.InnerException)
Throw New ManagedException("This action failed, please refresh and try again.")
End Try
Dim c = HttpContext.Current.Request.Cookies

令人难以置信的是,尽管之前进行了检查,但我现在在完全相同的行Dim c = HttpContext.Current.Request.Cookies上出现了错误。

最令人沮丧的是,API全天被多个客户端成功调用超过5000x,但其中一个客户端由于未知原因失败。

我在做一些测试时拒绝了cookie,但在这种情况下甚至无法登录系统,所以不可能是这样。

完全困惑,任何想法或建议都将受到欢迎。

原因1

Cookie只适用于回发。当没有完成请求时,它不起作用。

如果没有用户通过url请求操作,则cookie为空。

尝试从页面调用代码并控制事件。例如page.load/page.init

原因2

您的某个用户可能已禁用浏览器cookie。

最新更新