views:

57

answers:

1

Hello,

I am experimenting with AppFabric caching and have run into an issue with saving retrieved data in the cache. The root of the problem is that it seems that AppFabric Caching requires the data to have DataContract and Datamember attributes applied to the class that is being saved.

If that is the case, then how can I save this (simplified code):

var q = (from u in dbContext.Users
                         select new
                         {
                             Name = u.Name,
                             Id = u.RowId
                         });

cache.Put(“test”, q.ToList());

Calling Put causes this exception:

System.Runtime.Serialization.InvalidDataContractException was caught
 Message=Type '<>f__AnonymousTypec`6[System.Int32,System.String,System.Nullable`1[System.Int32],System.Boolean,System.Nullable`1[System.Int32],System.Int32]' cannot 
be serialized. Consider marking it with the DataContractAttribute attribute, and 
marking all of its members you want serialized with the DataMemberAttribute 
attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework 
documentation for other supported types.

How can I serialize the results of an IQueryable so AppFabric can cache it?

Thank you,

Rick

+1  A: 

It isn't that you're trying to do an IQueryable, it's that your type is Anonymous. Try making a class for your results and then creating one of those to store.

[Serializable]
public class UserIDPair
{
    public string Name {get;set;}
    public int ID {get;set;}
}

var q = (from u in dbContext.Users
    select new UserIDPair
    {
        Name = u.Name,
        Id = u.RowId
    });

cache.Put(“test”, q.ToList());

Make sure that the class is Serializable

Matthew Steeples
I thought of that, The only issue is that I will have dozens of these classes to maintain.
rboarman
How were you expecting to get the classes out of the cache again? Another alternative would be to XML serialize the object dynmically on save. You could do this with a helper method on the cache object.
Matthew Steeples