【发布时间】: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 你是如何存储会话的?