从会话状态读取数据时InvalidCastException异常 [英] InvalidCastException exception when reading from session state

查看:109
本文介绍了从会话状态读取数据时InvalidCastException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调试我的网站项目在Visual Studio 2013,我碰到一个问题,当我试着投了会话项特定类型(ZZZZ)。

When I debug my Web Site project in Visual Studio 2013 I get an issue when I try and cast a Session item to a specific type (ZZZZ).

return (ZZZZ) Session["SessionItem"];

这是引发InvalidCastException包含以下内容

An InvalidCastException is thrown with the following content

型的则''例外发生在
  APP_ code.axv-bzrg.dll但在用户code没有处理
       其他信息:无法转换类型'XXX.YYYY.ZZZZ对象键入'XXX.YYYY.ZZZZ

An exception of type 'System.InvalidCastException' occurred in App_Code.axv-bzrg.dll but was not handled in user code Additional information: Unable to cast object of type 'XXX.YYYY.ZZZZ' to type 'XXX.YYYY.ZZZZ'.

我添加了一个监视项目的

I added a watch item for

(WebUser) Session["LoggedInUser"]

其次为显示在手表的价值的部分:

And the following was displayed in the Value part of the watch:

该型'XXX.YYYY.ZZZZ'两个'APP_ code.axv-bzrg.dll'存在,并且
  APP_ code.ys_ymegf.dll

The type 'XXX.YYYY.ZZZZ' exists in both 'App_Code.axv-bzrg.dll' and 'App_Code.ys_ymegf.dll'

我浏览到临时ASP.NET文件文件夹

I navigated to the Temporary ASP.NET Files folder

C:\\用户_________ \\应用程序数据\\本地\\ TEMP \\临时ASP.NET
  文件\\ ROOT \\ dc8aa7ce \\ b2661c39

C:\Users_________\AppData\Local\Temp\Temporary ASP.NET Files\root\dc8aa7ce\b2661c39

和两个DLL文件都在那里。我认为APP_ code的DLL在每个页面请求生成,但没有被删除的previous那些导致那里是两个复制的类型的Temp目录。这是怎么回事,我该怎么去解决这个?

And the two DLL files were in there. I think the App_Code DLLs are generated on each page request, but the previous ones are NOT being removed causing there to be two duplicated types in the Temp directory. Why is this happening and how can I go about fixing this?

推荐答案

这是有些预期的行为。

您导航到网页第一次 - 对象添加到会话状态,键入 page_V1.dll:MyType的

You navigate to page first time - object added to session state with type page_V1.dll:MyType.

您决定要触及页面的源代码修复的东西 - 因为它不会杀死会话cookie浏览到同一页面将再次尝试从内存中的会话状态得到对象(会话状态也不会丢失不重新启动应用程序池由于只是页面的变化)。您在再次编译,现在这一点页尝试保存在会话新型 page_v2.dll案件类型:MyType的这的确失败,因为类型不彼此相关的(即使它们是相同的其他方式)。

You decide to touch source of the page to fix something - since it does not kill session cookies navigating to the same page again will try to get object from in-memory session state (session state is not lost as app pool not restarted due to just page changes). Your page at that point compiled again and now tries to case type stored in the session to new type page_v2.dll:MyType which indeed fail as types are not related to each other (even if they are identical otherwise).

修正:


  • 用于调试 - 启动新的浏览器会话/清除Cookie或只是触发应用程序池回收(即通过触摸的web.config)

  • 考虑移动对象类库项目,并从网站上添加参考吧 - 这样您控制类型实际上是重新编译,因而不会丧失,由于动态生成汇编文件名的身份

这篇关于从会话状态读取数据时InvalidCastException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆