ASP.NET cannot pass a session variable to a variable of the same type

I have an "almost random" error while trying to pass a session variable belonging to a type variable. Just to make it clear:

I have an Elemento class, I just create it and put it in my session variable:

Elemento elem = new Elemento(id, quantity); list.Add(elem); context.Session["cart"] = list; 

Now I need to restore the list, and I'm trying to do this with

 list = (List<Elemento>)context.Session["cart"]; 

Well .. it "someday" works, someday it doesn't! The first time I tried it, it worked flawlessly, but now I have a "500 internal server error" with this error:

Imposition [A] System.Collections.Generic.List 1[Elemento] a [B]System.Collections.Generic.List 1 [Elemento]. Il tipo A ha origine da 'mscorlib, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089' nel contesto 'LoadNeither' nella POSIZIONE "C: \ Windows \ assembly \ GAC_64 \ mscorlib \ 2.0.0.0____77775c561934l089 . Il tipo B ha origine da 'mscorlib, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089' nel contesto 'LoadNeither' nella POSIZIONE 'C: \ Windows \ build \ GAC_64 \ mscorlib \ 2.0.0.0__b77a5c561934eib. dll '.

translate it.

Cannot use [A] System.Collections.Generic.List 1[Elemento] to [B]System.Collections.Generic.List 1 [Elemento]. Type A is derived from 'mscorlib, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089' with the context "LoadNeither" and the position "C: \ Windows \ assembly \ GAC_64 \ mscorlib \ 2.0.0.0__b77a5c561934e089 \ mscorlib.dll. Type B has a start from 'mscorlib, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089' with the context 'LoadNeither' and the position 'C: \ Windows \ assembly \ GAC_64 \ mscorlib \ 2.0.0.0__b77a5c561934e089 \ mscorlib.dll' .

I can solve this β€œfor a while” by emptying the IIS cache, but it will happen again when I build the solution from visual studio.

I am reading in some place, I can solve it using interfaces .. but since I'm still learning how to use them, I can’t try right now, is there any proven solution for this?

EDIT: Works with krshekhar solution:

 list = context.Session["cart"] as List<Elemento>; 

Thank you!

+4
source share
2 answers

I believe this could be an IIS memory issue. Have you tried restarting IIS? Using a different IIS server?

I would recommend using other means to store objects such as cookies.

If you are thinking about scalability - Cookies will provide you with a better solution, and then a session object, because they are stored on the client, not on the server.

Good luck

0
source

Since you mention that you receive errors during the rebuild, it seems that there is a possibility that there are changes in the fields in the Elemento class or another class that it refers to, which disrupts the deserialization of the object from the state session.

When storing "complex" objects in an IIS session state, they are serialized using the BinaryFormatter (in the System.Runtime.Serialization.Formatters.Binary ). This formatter is sensitive to changes in serialized classes and cannot successfully deserialize an object graph if any of the classes in the graph have changed since the data was originally serialized. This seems to be the problem here if you say that it breaks after recovery.

I would suggest that you either keep only the types from BCL in session state (since they will only change between .NET versions). Or even better, implement your own serialization by implementing the ISerializable interface on your Elemento object, which will allow you to maintain compatibility between the builds of your project.

0
source

All Articles