问题
Problem: When caching an instance of a class and immediately getting it back out of cache, i get the object back (its not null), but all of its properties / fields are null or defaults.
_cacheHelper.PutInCache("testModuleControlInfoOne", mci);
//mci has populated fields
var mciFromCacheOne = _cacheHelper.GetFromCache("testModuleControlInfoOne");
//mciFromCacheOne now has null or default fields
So I suspect the way the object is structured is the problem and AppFabric is not serializing the object properly for some reason.
When I use the following Serialization method however, I get the object back with all properties / fields as they were prior to serialization.
public T SerializeThenDeserialize<T>(T o) where T : class
{
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, o);
ms.Position = 0;
return (T)bf.Deserialize(ms);
}
}
How can an object serialize and deserialize properly using the binary formatter and not do exactly the same thing via caching?
Has anyone encountered this or does anyone have any suggestions or tips on generally what to look out for?
回答1:
Ok found it.
The object implemented IXmlSerializable so AppFabric used that instead of the regular serialization.
Running it through an XmlSerializer (instead of a BinaryFormatter) gives the same null fields as I was experiencing.
It seems the IXmlSerializable implementation has issues.
回答2:
I believe when serializing to Xml (using IXmlSerializable), the private fields of an object are ignored, which may be why your object was incomplete upon retrieval.
Using Binary Serialization will insure the entire object (including references to other objects) are included.
回答3:
You may want to look at IDataCacheObjectSerializer
http://msdn.microsoft.com/en-us/library/windowsazure/hh552969.aspx
AppFabric Caching - Can I specify serialization style used for all objects?
(yes I realize this question was also yours :-)
来源:https://stackoverflow.com/questions/3732050/appfabric-caching-what-are-its-serialization-and-deserialization-requirements