AppFabric 缓存 - 对象的序列化和反序列化要求是什么?
问题:当缓存类的实例并立即将其从缓存中取出时,我取回了对象(它不为空),但其所有属性/字段均为空或默认值。
_cacheHelper.PutInCache("testModuleControlInfoOne", mci);
//mci has populated fields
var mciFromCacheOne = _cacheHelper.GetFromCache("testModuleControlInfoOne");
//mciFromCacheOne now has null or default fields
所以我怀疑对象的结构方式是问题所在,并且 AppFabric 由于某种原因没有正确序列化对象。
但是,当我使用以下序列化方法时,我会返回对象以及序列化之前的所有属性/字段。
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);
}
}
对象如何使用二进制格式化程序正确序列化和反序列化,而不是通过缓存执行完全相同的操作?
有没有人遇到过这种情况,或者有没有人对一般要注意的事项有任何建议或提示?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要查看
IDataCacheObjectSerializer
http: //msdn.microsoft.com/en-us/library/windowsazure/hh552969.aspx
AppFabric 缓存 - 我可以指定用于所有对象的序列化样式吗?
(是的,我意识到这个问题也是你的:-)
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 :-)
我相信当序列化为 Xml(使用 IXmlSerialized)时,对象的私有字段将被忽略,这可能就是您的对象在检索时不完整的原因。
使用二进制序列化将确保包含整个对象(包括对其他对象的引用)。
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.
好的找到了。
该对象实现了 IXmlSerialized,因此 AppFabric 使用它而不是常规序列化。
通过 XmlSerializer(而不是 BinaryFormatter)运行它会给出与我遇到的相同的空字段。
IXmlSerialized 实现似乎有问题。
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.