AppFabric缓存 - 我可以指定用于所有对象的序列化样式吗?

CRi*_*ice 8 configuration serialization caching appfabric appfabric-beta-2

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

我遇到了一个问题,当我放入缓存时,AppFabric在类上运行IXmlSerializable实现时,我宁愿强制它使用二进制. AppFabric缓存 - 对象的序列化和反序列化要求是什么?

我可以配置吗?

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

ken*_*ter 7

在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
    }
}
Run Code Online (Sandbox Code Playgroud)

之后,您可以将自定义序列化程序设置为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);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

  • 只是为了避免与任何人混淆 - 此解决方案仅适用于Windows azure缓存而不适用于appfabric缓存.MS非常努力地在产品名称中引起足够的混淆.这里的评论中有更多细节 - http://blogs.msdn.com/b/jagan_peri/archive/2012/08/23/custom-serializer.aspx (3认同)