ASP.NET 页面更改导致 Session 中的对象数组无法转换为其自己的类型?
我正在我的网站上的会话中存储自定义可序列化类的数组。当网站上的页面发生更改时,它会突然使它们无效,并告诉我它无法将类型转换为其自己的类型。我认为类版本号正在改变或者什么?!
我希望避免“不使用会话”答案,除非它是一个非常简单的解决方案。我并不是想重新设计整个过程。
Unable to cast object of type 'ShipmentPackages[]' to type 'ShipmentPackages[]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'ShipmentPackages[]' to type 'ShipmentPackages[]'.
Source Error:
Line 21: Else
Line 22: If Not Session("ShipmentList") Is Nothing Then
Line 23: ShipmentList = DirectCast(Session("ShipmentList"), ShipmentPackages()).ToList
Line 24: End If
Line 25: End If
I am storing an array of a custom serializable class in session on my site. When a page on the site changes, suddenly it renders them invalid, and tells me that it can't cast the type to it's own type. I assume the class version numbers are changing or something?!
I'd appreciate avoiding the "don't use session" answers, unless it's a really simple solution. I'm not trying to redesign this whole process.
Unable to cast object of type 'ShipmentPackages[]' to type 'ShipmentPackages[]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'ShipmentPackages[]' to type 'ShipmentPackages[]'.
Source Error:
Line 21: Else
Line 22: If Not Session("ShipmentList") Is Nothing Then
Line 23: ShipmentList = DirectCast(Session("ShipmentList"), ShipmentPackages()).ToList
Line 24: End If
Line 25: End If
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己已经多次看到这个消息了,非常烦人!正如您所指出的,这可能是因为程序集版本发生了变化。在 Asp.Net 中,当页面更改时,代码会重新编译。根据您放置类的位置,将确定该类是否与页面一起重新编译。我建议将任何“模型”类型的类移至单独的项目中。这将避免这个问题以及混合视图/控制器和模型代码的冲动:)。
您还可以尝试将对象作为 XML 序列化到会话中。如果这样做,即使程序集发生变化,您也应该能够反序列化它,但如果对象的属性发生变化则不能。
我知道您说过您不想听到这个,但您也可能会考虑不在会话中放置对象。如果有必要的话,这使得扩展您的应用程序变得困难。越早解决这个问题就越容易解决。
I have seen this message a number of times myself, it is very annoying! As you pointed out, it probably because the assembly version changed. In Asp.Net, when the page changes, the code gets recompiled. Depending on where you put the class will determine if the class gets recompiled with the page or not. I would suggest moving any "model" type classes to a separate project. This will avoid this problem as well as the urge to mix view/controller and model code :).
You can also try serializing the object into session as XML. If you do, you should be able to deserialize it even if the assembly changes, though not if the properties on the object change.
I know you said you didn't want to hear this, but you might also consider not putting objects in the session. This makes it difficult to scale your application if the time ever comes that it is necessary. The sooner you fix this the easier it will be to fix.
前几天我也被这个问题困扰了。唉,布莱恩的第一个解决方案只有在您不需要再次编译“模型项目”时才有效。如果您这样做(由于错误修复等)并更新正在运行的应用程序(用户在更新过程中保持会话,在我的情况下做了什么),您会再次遇到异常:-(!
在我的特殊情况下最好的解决方案非常简单!我将“DirectCast”更改为“TryCast”。如果程序集版本更改并且转换失败,则 trycast 将不会返回任何内容,或者如果我尚未将字典/集合写入会话,我(再次)通过数据库获取数据并随后存储,下次转换将起作用;-)!另一个要点是,如果对象的接口发生变化,这也适用!
A few days ago I got annoyed by this issue too. Alas Brian's first solution will only work as long as you do not need to compile the "model-project" again. If you do that (because of a bugfix, etc.) and update the running application (with users holding their session during the update-process, what is done in my case) you get the exception again :-(!
In my special case the best solution was really easy! I changed "DirectCast" to "TryCast". If the assemblyversion changed and casting fails, trycast will return nothing. In this case, or if I haven't written the dictionary/collection to the session yet, I fetch my data (again) over the database and store afterwards. The next time casting will work ;-)! And another great point, this works also if the interface of the object will change!