c# - 反序列化 System.Collections.ArrayList 类型的对象 - AppFabric 缓存错误

标签 c# asp.net appfabric appfabric-cache

我们有一个 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/

相关文章:

c# - C# 中动态类型的限制

c# - lambda 表达式

asp.net - 用于将 ASP.NET MVC 6 网站部署到 Azure 的脚本

asp.net - 在网站上存储和播放大视频?

.net - 是否可以调整 AppFabric 缓存服务器来存储更大的对象?

visual-studio-2010 - Windows Azure 中的 AppFabric 究竟是什么?

c# - 在使用 WebMatrix 环境的 C#.NET 网页中跨范围使用 GetLastInsertId() 是否安全?

c# - Visual Studio 2010 安装项目中的 "Runtime Implementation from ASSEMBLE NAME"是什么? (vs 2015 中的安装程序扩展设置项目)

asp.net - 链接按钮 CSS 在鼠标悬停时增加字体大小

asp.net - Azure 上的无 session ASP.NET