简体   繁体   English

Session_Start和ASP.Net异步SessionState模块

[英]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.

相关问题 ASP.net Session_Start方法中的依赖注入 - Dependency Injection in ASP.net Session_Start method 如果定义了Session_Start,ASP.NET如何知道创建ASP.NET_SessionId cookie? - How does ASP.NET know to create the ASP.NET_SessionId cookie if Session_Start is defined? 在asp.net中调用session_start或session_onstart时 - when session_start or session_onstart is called in asp.net 为什么我的ASP.NET MVC应用程序为单个会话多次触发Session_Start? - Why does my ASP.NET MVC application fire Session_Start multiple times for a single session? 在global.asax中从Session_Start调用Membership.GetUser()-ASP.NET身份 - Call Membership.GetUser() from Session_Start in global.asax - ASP.NET identity ASP.NET MVC-从Session_Start访问IoC容器 - Asp.Net mvc - accessing IoC container from Session_Start 如何在ASP.NET MVC中使用Session_Start? - How do I use Session_Start in ASP.NET MVC? Session_Start 在默认 ASP.NET MVC3 项目上多次触发 - Session_Start firing multiple times on default ASP.NET MVC3 project 无法将更改数据库保存在asp.net MVC C#的Global.asax的Session_Start()中 - Could not save change database in Session_Start() of Global.asax in asp.net MVC C# ASP.NET MVC 5 方法 Session_Start 在 IIS 中无法正常工作 - ASP.NET MVC 5 method Session_Start does not work properly in IIS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM