实现某些自定义序列化的对象可以序列化和反序列化为不同的格式,例如 Xml 或 byte[]。
我遇到了一个问题,当我放入缓存时,AppFabric 在类上运行 IXmlSerialized 实现,而我宁愿强制它使用二进制文件。AppFabric 缓存 - 对象的序列化和反序列化要求是什么?
我可以配置这个吗?
(目前的解决方法是以编程方式将对象序列化为 byte[],然后将其发送到缓存中,在退出时反转该过程)。
在 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);
希望这可以帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)