AppFabric缓存 - 我可以指定用于所有对象的序列化样式吗? [英] AppFabric Caching - Can I specify serialization style used for all objects?
问题描述
实现一些自定义序列化的对象可以序列化和反序列化为不同的格式,例如Xml或byte []。
An object which implements some custom serialization can be serialized and deserialized to different formats, for example to Xml or byte[].
当我放到缓存,AppFabric运行IXmlSerializable实现上一个类,当我宁愿强迫它与二进制。 AppFabric缓存 - 对某个对象的序列化和反序列化要求是什么?
I have run into a problem where when I put to cache, AppFabric runs the IXmlSerializable implementation on a class when I would rather force it to go with binary. AppFabric Caching - What are its serialization and deserialization requirements for an object?
我可以配置这个吗?
(现在解决方法是将对象序列化为byte [],然后将其发送到缓存中,
(At the moment the workaround is to serialize the object programatically to a byte[] and then send that into the cache, reversing the process on the way out).
推荐答案
在MSDN文档中,我们可以实现IDataCacheObjectSerializer来实现这个目标。您可以在这里阅读: http://msdn.microsoft.com/en-us/library /windowsazure/hh552969.aspx
In the MSDN documentation it says we could implement IDataCacheObjectSerializer to achieve this goal. You can read about it here: 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:
Afer that, you can set the custom serializer to the 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);
希望这有帮助。
这篇关于AppFabric缓存 - 我可以指定用于所有对象的序列化样式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!