AppFabric 缓存 - 对象的序列化和反序列化要求是什么?

发布于 2024-09-19 07:30:10 字数 879 浏览 4 评论 0原文

问题:当缓存类的实例并立即将其从缓存中取出时,我取回了对象(它不为空),但其所有属性/字段均为空或默认值。

    _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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小傻瓜 2024-09-26 07:30:55

您可能需要查看 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 :-)

演多会厌 2024-09-26 07:30:49

我相信当序列化为 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.

梦里的微风 2024-09-26 07:30:42

好的找到了。

该对象实现了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文