【问题标题】:Deserializing the object of type System.Collections.ArrayList- AppFabric Cache Error反序列化 System.Collections.ArrayList-AppFabric 缓存错误类型的对象
【发布时间】:2013-05-20 12:04:10
【问题描述】:

我们有一个 Web 应用程序,它经常在缓存内存中存储使用数据。
之前它是 HttpRuntime 缓存,但后来迁移到 AppFabric 缓存。
迁移后,尝试将对象添加到缓存中时会引发以下错误:
错误:

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; }
 }
}

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

【问题讨论】:

    标签: c# asp.net appfabric appfabric-cache


    【解决方案1】:

    对象以序列化形式存储在 AppFabric 缓存中。这意味着每个对象都必须是可序列化的。 AppFabric 在内部使用 NetDataContractSerializer。

    使用 HttpRuntime Cache 时,您只在缓存中保留一个引用,并且对象不会被序列化。

    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; 
    }
    

    【讨论】:

    • 感谢@Cyber​​maxs。非常非常有用的信息。如果我们可以选择编辑源类并且可以使用 Serializable 属性装饰该类将解决问题...?
    • NetDataContractSerializer 可用于任何标有 DataContractAttribute 或 SerializableAttribute 的类型,或实现 ISerializable 接口的类型。所以,你有三个选择!
    猜你喜欢
    • 2011-04-22
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 2018-10-25
    相关资源
    最近更新 更多