Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6909073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:38:35+00:00 2026-05-27T08:38:35+00:00

A site I’ve written is experiencing a strange issue whereby it suddenly doesn’t recognize

  • 0

A site I’ve written is experiencing a strange issue whereby it suddenly doesn’t recognize items stored in the session immediately after I’ve made any update to the code. I’ve searched Stackoverflow / google / etc and can see that some other people have the same problem but can’t find a solution anywhere.

Here’s roughly what’s happening:

If I add an item to my basket it stores a List<BasketItem> in the session. If I then make an update to some code (not BasketItem) the session variable still exists but .NET doesn’t seem to think the it’s a List<BasketItem> even though it definately is.

When I try and retrieve the list of basket items after modifying the code it throws an InvalidCastException which just doesn’t make any sense as it’s basically trying to say the types it’s converting between are different even though they are not.

The exception is:

System.InvalidCastException:
[A]System.Collections.Generic.List1[BasketItem] cannot be cast to
[B]System.Collections.Generic.List
1[BasketItem]. Type A originates
from ‘mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089’ in the context ‘LoadNeither’ at
location
‘C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll’.
Type B originates from ‘mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089’ in the context ‘LoadNeither’ at
location
‘C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll’.

I’m using StateServer for the session state and running ASP.NET 3.5 SP1 if that helps.

The code I’m using is below:

// for setting the basket
List<BasketItem> basketItems = new List<BasketItem>();
Session["basket"] = basketItems;

// for getting the basket
List<BasketItem> basketItems = (List<BasketItem>)Session["basket"];

For the moment I’m using ‘as casting’ so I doesn’t error but it does mean user’s basket’s are being lost when the code is updated.

Any advice would be much appreciated!

Cheers

Tim

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:38:35+00:00Added an answer on May 27, 2026 at 8:38 am

    This type of problem is pretty common due to how BinaryFormatter stores the underlying type data, which can cause problems if that they doesn’t resolve to exactly the same BasketItem that you had in mind. Most frequently, this hurts when changing the version of your application / library, or when there are different servers with different states.

    My strong advice here is: don’t let it use BinaryFormatter to store the state! It is not very version friendly, in both this regard and a few other issues within a type. If it is possible, I urge you to consider strong contract-based data instead (meaning: virtually anything other than BinaryFormatter / NetDataContractSerializer). Examples:

    • you could use JavaScriptSerializer and store a basic string of the data
    • you could use XmlSerializer and store a basic string of the data
    • if you want binary, you could use protobuf-net and strore a byte[] of the data

    You would obviously then use helper methods to store/retrieve your data, normally with a generic void Store<T>(string key, T object) and T Retreive<T>(string key), using typeof(T) internally as-necessary. The advantage of this is that the stored data is now neutral of any particular implementation, and can be consumed by other versions of your application (since nothing is type-dependent), and even by other platforms (Java, php, etc) if necessary.

    I appreciate this is a side-step around the problem rather than a direct solution – but: it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Site here. Basically the box in the middle doesn't generate random string from my
Site loads fine in IE8,9,FF & Chrome. I can't seem to remedy the issue
our site is written using Ruby on Rails. We are using coffeescript & SCSS.
Site hosted via IIS 7.0 I would like to set my session time-out to
The site is here: http://www.cottonbrewing.com/members It's fine if the content doesn't require the page
Site 1 has dll's for x amount of object and data calls. Can Site
site.com/link?p=2 give $_GET['p']==2 even though i've already made site.com/link rewrite to site.com/index.php?page=link So, i'm
My site has a drop-down menu built in CSS and JavaScript that drops down
My site has quite a deep navigation structure and quite often it looks like
A site I'm working on has Flash headers (using swfobject to embed them). Now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.