2

I'm getting error when I use azure caching but not using HttpRuntime.Cache:

Type 'System.Web.Services.Protocols.LogicalMethodInfo' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

After searching I noticed of this article that says "HttpRuntime.Cache doesn't serialize data at all"
What is the default serialization used by the ASP.net HttpRuntime.Cache
Sample Code:

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);
}
}
Community
  • 1
  • 1
piDev
  • 21
  • 1

1 Answers1

1

You're getting that error because System.Web.Services.Protocols.LogicalMethodInfo is not serializable. In order to be added into the Azure cache the object must be serialized and sent across the network to the cache server. Obviously that can't be done with non-serializable objects. HttpRuntime.Cache is an in-memory data store. Because it's in-memory it doesn't need to serialize the objects, and hence doesn't care whether the cached objects are serializable.

Brian Reischl
  • 7,216
  • 2
  • 35
  • 46