2011-06-16 51 views
3

我得到了有一个自定义类辞书,并列出了例外。 例子:随机InvalidCastException的

List<DisplayAllQuestionsTable> dsa = (List<DisplayAllQuestionsTable>)Session["Display"]; 

演员适用于10〜20倍,当我使用Session..and然后它开始抛出异常。如果我活着的电脑大约20-30分钟..我可以像往常一样启动我的Web应用程序,并在启动代码20次后,它会抛出相同的异常。为什么会发生?

现在我测试了另一种更简单的代码与雪村:

public partial class Default2 : System.Web.UI.Page 
{ 
    List<meem> moom = new List<meem>(); 
    protected void Page_Load(object sender, EventArgs e) 
    { 


     for (int i = 0; i < 20; i++) 
      { 
       meem m = new meem(); 
       m.i = i; 
       moom.Add(m); 
      } 



     Session["meem"] = moom; 
     Button ew = new Button(); 
     ew.Text = "Press me"; 
     ew.Click += Click; 
     PlaceHolder1.Controls.Add(ew); 
    } 
    void Click(object sender, EventArgs e) 
    { 
     List<meem> moom = (List<meem>)Session["meem"]; 
     foreach (var item in moom) 
     { 
      Label l = new Label(); 
      l.Text = item.i.ToString(); 
      this.Controls.Add(l); 
     } 

    } 



} 

class meem 
{ 
    public int i; 
} 

和它的作品100%

,我得到异常:

Server Error in '/WebSite10' Application. 
[A]System.Collections.Generic.List`1[DisplayAllQuestionsTable] cannot be cast to 
[B]System.Collections.Generic.List`1[DisplayAllQuestionsTable]. 
Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' 
    at location 'D:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. 
Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' 
    at location   'D:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. 
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: [A]System.Collections.Generic.List`1[DisplayAllQuestionsTable] cannot be cast to [B]System.Collections.Generic.List`1[DisplayAllQuestionsTable]. Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'D:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'D:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. 
+0

想必有人顺手把东西进入会议[“显示”。当你得到异常时,你可以检查Session [“Display”]中的内容。 – 2011-06-16 10:16:50

+0

如何进行一些调试并检查Session [“Display”]的值是什么时候爆炸? – 2011-06-16 10:17:02

+0

您的会话是否超时?你的AppPool被销毁了吗? – 2011-06-16 10:18:13

回答

0

听起来像您的会话已逾时。 (默认为20分钟)

在你的点击处理程序首先检查会话对象存在,或迭代它之前是空的。

UPDATE

看了以后你的异常细节。这篇文章会有帮助吗? InvalidCastException when serializing and deserializing同时检查标记答案中的“加载器上下文”链接。

希望这将帮助你进一步跟踪异常。

+0

听起来不像那样,(List )null不会抛出InvalidCastException。 – 2011-06-16 10:32:41

+0

我不认为这是答案。我设置了一个if语句if(Session [“Display”]!= null)... session exists ..所以是例外...那个会话对象和页面生命周期使我疯狂。可能是我应该用t-sql做整件事情? – Matrix001 2011-06-16 10:32:45

+0

也考虑到我用“meem”类测试的简单代码的工作原理,无论我运行多少次代码 – Matrix001 2011-06-16 10:34:47

1

这个代码就是List<DisplayAllQuestionsTable> dsa = (List<DisplayAllQuestionsTable>)Session["Display"];

不会造成只有当你试图使用它一个空引用异常。

同样,如果Session["test"]为null,该代码List<test> l = (List<test>)Session["test"];不会造成null或无效的转换异常。如果Session["test"]不为空,则只会发生无效的转场异常。在我看来,存储在显示器中的对象在某种程度上已经变形。

+0

这是对象的歌剧魅影!另外,嘿,考虑使用编辑器中的相关工具栏按钮正确地设置您的响应格式。 =) – 2011-06-16 10:48:18

+0

大声笑..以及对我来说意味着我放置1周写的所有mvc代码都必须放弃,如果有人不提供答案......我太亲近了让这个东西工作..只有没有那个愚蠢的异常被抛出:( – Matrix001 2011-06-16 10:49:24

+0

我的模式是inProc – Matrix001 2011-06-16 13:10:00