1
我試圖將IEnumerable<T>
存儲到memcached中,但是,我只能成功存儲T
的實例。如何在memcached中存儲IEnumerable <T>的實例?
是否有不同的代碼在memcached中存儲枚舉?
public IEnumerable<UsContentView> GetContentViewByUserId(int userId)
{
Expire("contentViewUserId_" + userId);
var result = Memcached.Get<IEnumerable<UsContentView>>("contentViewUserId_" + userId);
if (result == null)
{
result = db.UsContentViews.Where(m => m.UserID == userId).OrderBy(m => m.ArticleId).Distinct();
var arr = result.ToArray();
var arrList = arr.ToList();
//store it in the cache, with the key
StoreList(arrList, "contentViewUserId_" + userId);
MemoryStream mem = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
try
{
b.Serialize(mem, result);
}
catch (EntitySqlException ex)
{
throw new ApplicationException("The object query failed", ex);
}
catch (EntityCommandExecutionException ex)
{
throw new ApplicationException("The object query failed", ex);
}
catch (SerializationException ex)
{
throw new ApplicationException("The object graph could not be serialized", ex);
}
}
return result;
}
我做到了。但仍然在'/'應用程序中出現此服務器錯誤。 'Assembly in'Telerik.OpenAccess.35.Extensions,Version = 2012.2''類型'Telerik.OpenAccess.Query.Piece'1 [[Desktop_ORM.UsContentView,Desktop_ORM,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]' .607.1,Culture = neutral,PublicKeyToken = 7ce17eeaf1d59342'未標記爲可序列化。 – Tun 2012-07-31 15:33:57
您只能提供可序列化的類型,因此請使用'SerializableAttribute'屬性標記您嘗試存儲的類。 – 2012-07-31 15:36:10
我做到了。只有在使用IEnumerable時纔會出現此錯誤。 –
Tun
2012-07-31 16:36:29