2

Here's my bit of code:

List<Sale> sales = new List<Sale>();

if (Cache["Sales"] != null)
{
    sales = (List<Sale>)Cache["Sales"];
}
else
{
    ...
    Cache.Add("Sales", sales, null, DateTime.Now.AddMinutes(20),
        Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
}

When I try to pull the data from the cache, my "sales" object is null. Wondering why that code is hit at all, I ran the debugger in VS to see what was in the Cache object.

The Cache contains the data I need, but when it gets the data from cache, "sales" still comes out as null.

Is there something I'm doing wrong here?

EDIT:

I'm getting this error on casting:

[A]System.Collections.Generic.List1[controls_mySales+Sale] cannot be cast to [B]System.Collections.Generic.List1[controls_mySales+Sale]. Type A originates from 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\WINDOWS\assembly\GAC_32\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_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll'

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Steven
  • 18,761
  • 70
  • 194
  • 296
  • Is that the only place in your codebase where you assign a value to that cache key? – Oded Sep 28 '10 at 18:41
  • 1
    Have you tried doing a cast to `List` as opposed to using the `as` operator? Perhaps there's a casting issue. – ngroot Sep 28 '10 at 18:43
  • What happens if you store a `Sale` instead of a `List`? What casting error do you get? – SLaks Sep 28 '10 at 18:52

3 Answers3

3

MSDN's page on the as keyword states that:

The as operator is like a cast except that it yields null on conversion failure instead of raising an exception.

Looks this is what's happening here -- the cast to type List<Sale> is failing, and returning null. Are you sure this is the type of the object in your cache?

EDIT:

In response to your edit, it seems like some sort of assembly-related serialization/deserialization issue possible related to binding contexts that honestly is a little over my head. I checked around and found the following two questions here on SO that may be able to point you in the right direction:

Question 1
Question 2

Hopefully those links prove helpful.

Community
  • 1
  • 1
Donut
  • 110,061
  • 20
  • 134
  • 146
  • Nice research. I've edited my original post showing the exception being thrown. Do you know what's wrong with that? – Steven Sep 28 '10 at 18:47
  • This is a known issue, for more http://www.dotshoppingcart.com/View/Forum/DotShoppingCart%20Forums/Support/Using%20DotShoppingCart/369.aspx – pokrate Jun 05 '11 at 21:05
0

There are obviously two classes Sale.

It looks like a nested class in a dynamically compiled Page or UserControl. Difficult to understand exactly what's going on without seeing more code, but perhaps when the Page/UserControl is recompiled for some reason, it gets a new type.

I'd move the Sale class out into its own non-nested class in its own source file.

Joe
  • 122,218
  • 32
  • 205
  • 338