简体   繁体   English

SessionID抛出空异常

[英]SessionID throws null exception

I have an object that encapsulates a System.Web.SessionState.HttpSessionState object as one of its data members. 我有一个将System.Web.SessionState.HttpSessionState对象封装为其数据成员之一的对象。 Rarely (to the point where we are unable to reproduce exactly when or how) we will get a NullReferenceException error with this object when accessing customSession.Session.SessionID 很少(以至于我们无法精确地复制时间或方式),在访问customSession.Session.SessionID时,此对象将收到NullReferenceException错误

I've used the debugger to trace the object, and customSession.Session is not null. 我已经使用调试器来跟踪对象,并且customSession.Session不为null。 It is a valid HttpSessionState object. 它是有效的HttpSessionState对象。

Mode: InProc

StaticObjects: {System.Web.HttpStaticObjectsCollection}

SyncRoot: {System.Web.SessionState.HttpSessionStateContainer}

Keys: {System.Collection.SpecializedNameObjectCollectionBase.KeysCollection}

IsNewSession: true

IsReadOnly: false

Keys has 4 records in it. 键中有4条记录。

But even in the debugger, if I explore the object and try to get the SessionID, it says 但是即使在调试器中,如果我浏览对象并尝试获取SessionID,它也会说

"cs.Session.SessionID threw an exception of type System.NullReferenceException. “ cs.Session.SessionID抛出了System.NullReferenceException类型的异常。

Does anyone know if this is just a bug in Visual Studio's development server? 有谁知道这是否只是Visual Studio开发服务器中的错误? I just don't want to have this issue occur on a production release and the only way to fix it is to restart IIS. 我只是不想在生产版本上发生此问题,解决此问题的唯一方法是重新启动IIS。

-- Edit -- -编辑-

Additional information: 附加信息:

I'm using the "break when common language runtime exception is thrown" option, so what's happening is the system is trying to access the CustomSession object, as cs.Session.SessionID and getting a null-ref exception, and the debugger kicks in. Then I use the watch window and put in "cs.Session" in the watch, and it shows all the properties of the Session object, and the line for SessionID says it throws a null reference exception. 我正在使用“抛出公共语言运行时异常时中断”选项,所以发生的事情是系统正在尝试访问CustomSession对象(如cs.Session.SessionID并获取null-ref异常),并且调试器启动然后,我使用监视窗口并在监视窗口中放入“ cs.Session”,它显示了Session对象的所有属性,并且SessionID的行表示它引发了空引用异常。 This null ref exception causes the application to break if not manually handled. 如果没有手动处理,则此null ref异常会导致应用程序中断。

I was able to reproduce the error by modifying code in the App_Code directory repeatedly (took about 5 or 6 changes I think). 通过重复修改App_Code目录中的代码,我能够重现该错误(我认为需要进行5或6次更改)。 I don't know for certain if this is the only way of this issue happening as others here have reported it happening after what they suspected was a session timeout. 我不确定这是否是此问题发生的唯一方法,因为这里的其他人报告说,他们怀疑是会话超时后才发生。 In any case, it seems to be happening within the "Session_End" event coming out of the global.asax. 无论如何,这似乎是在global.asax发出的“ Session_End”事件内发生的。 But the error occurs before the "Session.Abandon()" statement is hit. 但是该错误发生在命中“ Session.Abandon()”语句之前。

I have confirmed that I can access values stored in the session. 我已经确认可以访问会话中存储的值。 The only thing giving an error is accessing the SessionID. 唯一出错的是访问SessionID。

--- Additional (oct30) ---- -附加(oct30)----

The line where the error occurs is the if-statement: 发生错误的行是if语句:

foreach (CustomSession cs in allSessions) {
    if (cs.Session.SessionID == curSession.Session.SessionID) {
        // do stuff
    }
}

The "cs" and "curSession" objects are both not null, the "Session" property of each is also not null at this point. “ cs”和“ curSession”对象都不都是空,此时每个对象的“ Session”属性也不是空。

You are watching an exception which would be handled ordinarily by the framework. 您正在观看一个通常由框架处理的异常。

Essentially it is implementing a per-connection singleton pattern using the exception to detect when the session does not exist. 本质上,它正在使用异常来实现每个连接的单例模式,以检测会话何时不存在。 So it just uses the session object, and if that throws an exception, it creates one on the spot and carries on. 因此,它仅使用会话对象,并且如果该对象引发异常,它将立即创建一个并继续。

Since you have selected "break when exception is thrown" and presumably not selected "Just my code", it is breaking. 由于您已选择“抛出异常时中断”,并且可能未选择“仅我的代码”,因此它正在中断。 Because you asked it to. 因为你要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM