tags:

views:

42

answers:

1

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'

+3  A: 

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.

Donut
Nice research. I've edited my original post showing the exception being thrown. Do you know what's wrong with that?
Steven
Thanks for the help.
Steven