我正在尝试使用AppFabric缓存,并且遇到了将检索到的数据保存在缓存中的问题。问题的根源在于AppFabric缓存似乎要求数据将DataContract和Datamember属性应用于正在保存的类。
如果是这种情况,那么我该如何保存(简化代码):
var q = (from u in dbContext.Users
select new
{
Name = u.Name,
Id = u.RowId
});
cache.Put(“test”, q.ToList());
Calling Put导致此异常:
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可以缓存它?
谢谢,
瑞克
答案 0 :(得分: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());
确保该类是Serializable