【问题标题】:AppFabric Caching - Can I specify serialization style used for all objects?AppFabric 缓存 - 我可以指定用于所有对象的序列化样式吗?
【发布时间】:2011-04-14 23:07:36
【问题描述】:

实现了一些自定义序列化的对象可以被序列化和反序列化为不同的格式,例如 Xml 或 byte[]。

我遇到了一个问题,当我放入缓存时,AppFabric 会在一个类上运行 IXmlSerializable 实现,而我宁愿强制它使用二进制文件。 AppFabric Caching - What are its serialization and deserialization requirements for an object?

我可以配置吗?

(目前的解决方法是以编程方式将对象序列化为 byte[],然后将其发送到缓存中,在退出时反转过程)。

【问题讨论】:

    标签: serialization configuration caching appfabric appfabric-beta-2


    【解决方案1】:

    在 MSDN 文档中,它说我们可以实现 IDataCacheObjectSerializer 来实现这个目标。你可以在这里阅读:http://msdn.microsoft.com/en-us/library/windowsazure/hh552969.aspx

    class MySerializer : IDataCacheObjectSerializer
    {
        public object Deserialize(System.IO.Stream stream)
        {
            // Deserialize the System.IO.Stream 'stream' from
            // the cache and return the object 
        }
    
        public void Serialize(System.IO.Stream stream, object value)
        {
            // Serialize the object 'value' into a System.IO.Stream
            // that can be stored in the cache
        }
    }
    

    之后,您可以将自定义序列化程序设置为 DataCacheFactory:

    DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration();
    
    configuration.SerializationProperties = 
       new DataCacheSerializationProperties(DataCacheObjectSerializerType.CustomSerializer, 
       new MyNamespace.MySerializer());
    
    // Assign other DataCacheFactoryConfiguration properties...
    
    // Then create a DataCacheFactory with this configuration
    DataCacheFactory factory = new DataCacheFactory(configuration);
    

    希望这会有所帮助。

    【讨论】:

    • 谢谢!这将挽救我的生命!
    • 只是为了避免让任何人感到困惑 - 此解决方案仅适用于 Windows azure 缓存,不适用于 appfabric 缓存。 MS 非常努力地在他们的产品名称中引起足够的混乱。更多细节在 cmets 这里 - blogs.msdn.com/b/jagan_peri/archive/2012/08/23/…
    猜你喜欢
    • 2011-04-22
    • 1970-01-01
    • 2013-02-19
    • 2010-11-13
    • 1970-01-01
    • 2011-04-13
    • 2014-09-13
    • 2014-10-15
    • 1970-01-01
    相关资源
    最近更新 更多