3

I get that exception in Dictionaries and Lists that have a custom class. Example:

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

The cast works for 10-20 times when i use the Session..and then it starts to throw the exception. If i live the pc on for about 20-30 mins..i can launch my web application as usual, and after 20 times of launching the code, it throws the same exception. Why does that happen?

Now i tested another more simple code with 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;
}

And it works 100%

The exception that I get:

    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'.
jgauffin
  • 99,844
  • 45
  • 235
  • 372
Matrix001
  • 1,272
  • 6
  • 30
  • 51
  • Presumably someone is shoving something else into Session["Display"]. When you get the exception you can check what's inside Session["Display"]. – Andrew Barrett Jun 16 '11 at 10:16
  • What about doing some debugging and checking what the value of Session["Display"] is when it blows up? – R. Martinho Fernandes Jun 16 '11 at 10:17
  • Is your session timing out? Is your AppPool being destroyed? – agent-j Jun 16 '11 at 10:18
  • Please post the exception text, since it gives the type of object that failed to be casted. – agent-j Jun 16 '11 at 10:19
  • Are you running this via the ASP.NET development server? It sounds like the development server isn't cleaning up the session after rebuild properly or something strange. – Andrew Barrett Jun 16 '11 at 10:48
  • 1
    Yeah, but you would expect if you rebuild the application with a new definition for DisplayAllQuestionsTable it will nuke the Session if the session is stored InProc. Matrix001 how are you storing the session? – Andrew Barrett Jun 16 '11 at 10:54
  • I just type Session["StoreDictionary"]=dictionaryInstance; Thats all. i rebuild the code with ctrl+shift+B... how can i avoid the sesson from being nuked? i do put new definition to Session.. should i clear the session and then put new definitions??? Is that what you mean.. Do i overload the session for some reason? – Matrix001 Jun 16 '11 at 12:21
  • Are you experiencing this error when hosting the app in IIS or are you using the ASP.NET development server? – Andrew Barrett Jun 16 '11 at 15:22

2 Answers2

1

This code as is, List<DisplayAllQuestionsTable> dsa = (List<DisplayAllQuestionsTable>)Session["Display"];

will not cause a null reference exception only if you tried to use it.

Similarly this code List<test> l = (List<test>)Session["test"]; will not cause a null or invalid cast exception if Session["test"] is null. An invalid cast exception will only occur if Session["test"] is not null. It seems to me the object stored in Display has been deformed in someway.

Community
  • 1
  • 1
Syed Hussim
  • 720
  • 1
  • 4
  • 16
  • It's the Phantom of the Opera of objects! Also, hey, consider formatting your response correctly using the relevant toolbar buttons in the editor. =) – J. Steen Jun 16 '11 at 10:48
  • lol..and what it means to me that all the mvc code that i put 1 week to write has to be abandoned, if someone doesnt come up with an answer... i was so close to get this thing to work.. only without that stupid exception being thrown :( – Matrix001 Jun 16 '11 at 10:49
0

Sounds like your session timedout. (default is 20 minutes)

In your click handler first check if the session object exists or is null before iterating over it.

UPDATE

Saw your exception details later on. Will this post be helpfull? InvalidCastException when serializing and deserializing Also check the 'loader contexts' link in the marked answer.

Hope this will help you track your exception further.

Community
  • 1
  • 1
ChristiaanV
  • 5,401
  • 3
  • 32
  • 42
  • It doesn't sound like that, (List)null wont throw an InvalidCastException. – Andrew Barrett Jun 16 '11 at 10:32
  • i dont think it is the answer. I set an if statement if(Session["Display"] != null )... session exists.. and so is the exception... That session object and page life cycle drives me nuts. may be i should do the whole thing with t-sql? – Matrix001 Jun 16 '11 at 10:32
  • Also take into account that the simple code that i tested with "meem" class works ,,no matter how many times i run my code – Matrix001 Jun 16 '11 at 10:34