【问题标题】:WebAPI HttpContext.Current.Request.Cookies NULL despite checksWebAPI HttpContext.Current.Request.Cookies NULL 尽管检查
【发布时间】:2021-03-25 10:04:17
【问题描述】:

在过去的一年里,我一直在开发一个可以毫无问题地调用 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 在一天之内被多个客户端成功调用超过 5000 次,而一个未知原因却失败了。

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

完全困惑,欢迎任何想法或建议。

【问题讨论】:

    标签: vb.net cookies asp.net-apicontroller


    【解决方案1】:

    原因 1

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

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

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

    原因 2

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

    【讨论】:

      猜你喜欢
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多