当我使用azure缓存但没有使用HttpRuntime.Cache时出现错误:
无法序列化类型“ System.Web.Services.Protocols.LogicalMethodInfo ”。请考虑使用DataContractAttribute属性对其进行标记,并使用DataMemberAttribute属性标记要序列化的所有成员。如果类型是集合,请考虑使用CollectionDataContractAttribute对其进行标记。有关其他受支持的类型,请参阅Microsoft .NET Framework文档。
搜索后我注意到这篇文章说“HttpRuntime.Cache根本没有序列化数据”
What is the default serialization used by the ASP.net HttpRuntime.Cache
示例代码:
protected void Page_Load(object sender, EventArgs e)
{
List<myclass> items = new List<myclass>();
MyClass item1 = new MyClass() { ID = 1 };
items.Add(item1);
HttpRuntime.Cache.Insert("test1", items); //working
DataCache cache = new DataCache("default");
cache.CreateRegion("reg");
cache.Put("test2", items, "reg");//error
}
}
public class MyClass
{
public MyClass()
{
Type myType = typeof(MyService);
MethodInfo myMethodInfo = myType.GetMethod("Add");
_log = new **LogicalMethodInfo**(myMethodInfo);
}
public int ID { get; set; }
public **LogicalMethodInfo** _log;
}
public class MyService
{
public int Add(int xValue, int yValue)
{
return (xValue + yValue);
}
}
答案 0 :(得分:1)
您收到该错误是因为System.Web.Services.Protocols.LogicalMethodInfo
不可序列化。为了添加到Azure缓存中,必须将对象序列化并通过网络发送到缓存服务器。显然,无法使用不可序列化的对象。 HttpRuntime.Cache
是内存中的数据存储。因为它在内存中,所以不需要序列化对象,因此不关心缓存的对象是否可序列化。