我们有一个 web 应用程序,它在缓存内存中存储频繁使用的数据。
之前它是 HttpRuntime Cache,但后来迁移到 AppFabric Cache。
迁移后,它在尝试将对象添加到缓存中时抛出以下错误:
错误:
System.Runtime.Serialization.SerializationException:
"There was an error deserializing the object of type
System.Collections.ArrayList. No set method for property '' in type ''."
添加到 HttpRuntime 缓存仍在工作。但是对 AppFabric Cache 抛出上述错误。
将项目添加到缓存内存的代码片段:
public static void Add(string pName, object pValue)
{
//System.Web.HttpRuntime.Cache.Add(pName, pValue, null, DateTime.Now.AddSeconds(60), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
appFabricCache.Add(pName, pValue);
}
以下类的实例正在尝试存储在高速缓存中。
public class Kernel
{
internal const BusinessObjectSource BO_DEFAULT_SOURCE=BusinessObjectSource.Context;
private System.Collections.ArrayList mProcesses = new System.Collections.ArrayList();
private System.Collections.Hashtable mProcessesHash = new System.Collections.Hashtable();
public SnapshotProcess mSnapShotProcess ;
private System.Collections.ArrayList mErrorInformation;
public Collections.ArrayList Processes
{
get { return mProcesses; }
}
}
任何人都知道如何解决这个问题......?谢谢。
最佳答案
对象以序列化形式存储在 AppFabric 缓存中。这意味着每个对象都必须是可序列化的。 AppFabric 在内部使用 NetDataContractSerializer。
使用 HttpRuntime 缓存 时,您只在缓存中保留一个引用,对象不会被序列化。
System.Collections.ArrayList
(非常古老的类)是可序列化的,但每个嵌套/子级也必须是可序列化的。因此,以这种方式更改您的代码(内核和嵌套/子类型)。
这是一段代码,用于在没有 AppFabric 的情况下测试序列化。
// requires following assembly references:
//
//using System.Xml;
//using System.IO;
//using System.Runtime.Serialization;
//using System.Runtime.Serialization.Formatters.Binary;
//
// Target object “obj”
//
long length = 0;
MemoryStream stream1 = new MemoryStream();
using (XmlDictionaryWriter writer =
XmlDictionaryWriter.CreateBinaryWriter(stream1))
{
NetDataContractSerializer serializer = new NetDataContractSerializer();
serializer.WriteObject(writer, obj);
length = stream1.Length;
}
关于c# - 反序列化 System.Collections.ArrayList 类型的对象 - AppFabric 缓存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16649124/