2013-05-20 50 views
2

我们有一个Web应用程序,它将经常使用的数据存储在缓存中。
此前它是HttpRuntime缓存,但后来迁移到AppFabric缓存。
迁移后,它会引发以下错误,而试图将一个对象添加到高速缓存:
错误:
反序列化System.Collections.ArrayList类型的对象 - 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; } 
} 
} 

任何人都知道如何解决这个问题......?谢谢。

回答

1

对象以序列化形式存储在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; 
} 
+0

谢谢@Cyber​​maxs。非常非常有用的信息。如果我们有一个编辑源类的选项,并且可以使用Serializable属性来修饰该类将解决问题...? – Sunil

+1

NetDataContractSerializer可用于任何标有DataContractAttribute或SerializableAttribute的类型或实现ISerializable接口的类型。所以,你有三个选择! – Cybermaxs