[英]Session_Start and ASP.Net Async SessionState Module
We recently integrated the ASP.Net Async SessionState Module and have started seeing null ref exceptions in our Global.asax Session_Start event handler. 我们最近集成了ASP.Net Async SessionState模块 ,并开始在Global.asax Session_Start事件处理程序中看到null ref异常。
I can't replicate it locally, and it doesn't appear to happen all the time in live, but I believe this is when we attempt to access HttpContext.Current in Session_Start. 我无法在本地复制它,并且它似乎并非一直都在实时发生,但是我相信这是当我们尝试在Session_Start中访问HttpContext.Current时。 My guess is HttpContext.Current is sometimes null, because Session initialization is asynchronous. 我的猜测是HttpContext.Current有时为null,因为Session初始化是异步的。
Any suggestions as to how to address? 关于如何解决有什么建议吗?
Maybe the answer is too simple, but I have also seen this from time to time, and I'd suggest that you safeguard your code inside the Session_Start
event like 也许答案太简单了,但我有时也会看到这种情况,建议您像这样在Session_Start
事件中保护代码
if (HttpContext.Current !== null)
{
// do your actions with the Current object
}
else
{
// possibly add some logging here to see what's going on
}
If you notice it is just a race condition, your code will react properly and not just end up in a NullReferenceException. 如果您发现这只是一个竞争条件,那么您的代码将做出正确的反应,而不仅仅是出现NullReferenceException。
Safeguarding it like this is in this particular case better than adding Elvis operators ( ?.
) everywhere you're referencing it because that will lead into more complex scenarios for your testing/troubleshooting. 在这种特殊情况下,像这样保护它比在您引用它的任何地方添加Elvis运算符( ?.
)更好,因为这将导致测试/故障排除的情况更加复杂。 In other cases, however, this operator is quite useful, for example in this different context. 但是,在其他情况下,例如在不同的上下文中 ,此运算符非常有用。
Additionally, In the link you provided, I saw the hint "To implement your own async sessionstate provider which works with Microsoft.AspNet.SessionState.SessionStateModule, all you need to do is to implement a concrete SessionStateStoreProviderAsyncBase class which is included in the Microsoft.AspNet.SessionState.SessionStateModule NuGet package." 此外,在您提供的链接中,我看到了提示“要实现与Microsoft.AspNet.SessionState.SessionStateModule一起使用的自己的异步sessionstate提供程序,您所需要做的就是实现一个具体的SessionStateStoreProviderAsyncBase类,该类包含在Microsoft中。 AspNet.SessionState.SessionStateModule NuGet程序包。”
Maybe you just need to implement that class rather than using Session_Start - because here is always a HttpContext given as parameter and Session_Start is just there for backwards-compatibility? 也许您只需要实现该类而不是使用Session_Start-因为这里总是将HttpContext作为参数提供,而Session_Start只是为了向后兼容?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.