2010-10-07 91 views
0

我正在试验AppFabric缓存,并遇到了将缓存中的检索数据保存的问题。问题的根源在于AppFabric Caching似乎要求数据具有DataContract和Datamember属性应用于正在保存的类。AppFabric缓存和序列化IQueryable对象

如果是这样的话,那我怎么才能拯救这个(简化代码):

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

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

调用看跌导致此异常:

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. 

我如何可以序列将IQueryable的结果所以AppFabric可以缓存它?

谢谢

里克

回答

1

这不是你想要做一个IQueryable,那就是你的类型是匿名的。尝试为你的结果做一个类,然后创建一个存储。

[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()); 

确保类是可序列

+0

我想到这一点,唯一的问题是,我将有几十这些类的维护。 – rboarman 2010-10-07 23:52:55

+0

您是如何期望再次从缓存中取出课程的?另一种选择是XML在保存时序列化对象。你可以在缓存对象上用辅助方法来做到这一点。 – 2010-10-08 12:30:34