【问题标题】:Random InvalidCastException随机 InvalidCastException
【发布时间】:2011-06-16 10:13:09
【问题描述】:

我在具有自定义类的字典和列表中遇到了该异常。 示例:

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

当我使用 Session.. 然后它开始抛出异常时,演员表工作了 10-20 次。如果我在电脑上运行大约 20-30 分钟..我可以像往常一样启动我的 Web 应用程序,并且在启动代码 20 次后,它会抛出相同的异常。为什么会这样?

现在我用 Sesson 测试了另一个更简单的代码:

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'.

【问题讨论】:

  • 可能有人正在将其他东西推入 Session["Display"]。当您收到异常时,您可以检查 Session["Display"] 中的内容。
  • 如何进行一些调试并检查 Session["Display"] 爆炸时的值是多少?
  • 您的会话超时了吗?你的 AppPool 被销毁了吗?
  • 请发布异常文本,因为它给出了无法转换的对象类型。
  • 是的,但是您会期望,如果您使用 DisplayAllQuestionsTable 的新定义重建应用程序,如果会话存储在 InProc 中,它将破坏会话。 Matrix001 你是如何存储会话的?

标签: c# session casting


【解决方案1】:

此代码原样,List&lt;DisplayAllQuestionsTable&gt; dsa = (List&lt;DisplayAllQuestionsTable&gt;)Session["Display"];

仅当您尝试使用它时才会导致空引用异常。

同样,如果 Session["test"] 为 null,则此代码 List&lt;test&gt; l = (List&lt;test&gt;)Session["test"]; 不会导致 null 或无效强制转换异常。仅当Session["test"] 不为空时,才会发生无效转换异常。在我看来,存储在 Display 中的对象已经以某种方式变形了。

【讨论】:

  • 这是对象的歌剧魅影!另外,嘿,考虑使用编辑器中的相关工具栏按钮正确格式化您的响应。 =)
  • 大声笑..如果有人没有给出答案,我花了 1 周时间编写的所有 mvc 代码都必须被放弃,这对我意味着什么......我是如此接近让这件事起作用..只有在没有抛出那个愚蠢的异常的情况下:(
【解决方案2】:

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

在您的点击处理程序中,首先检查会话对象是否存在或是否为空,然后再对其进行迭代。

更新

稍后看到您的异常详细信息。这篇文章会有帮助吗? InvalidCastException when serializing and deserializing 还要检查标记答案中的“加载器上下文”链接。

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

【讨论】:

  • 听起来不像,(List)null 不会抛出 InvalidCastException。
  • 我不认为这是答案。我设置了一个 if 语句 if(Session["Display"] != null )... session 存在.. 例外情况也是如此... session 对象和页面生命周期让我发疯。可能我应该用 t-sql 做整个事情吗?
  • 还要考虑到我用“meem”类测试的简单代码,不管我运行多少次代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多