Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppFabric Caching - Can I specify serialization style used for all objects?

An object which implements some custom serialization can be serialized and deserialized to different formats, for example to Xml or byte[].

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?

Can I configure this?

(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).

like image 731
CRice Avatar asked Sep 21 '10 00:09

CRice


People also ask

Can you customize serialization?

Customized serialization can be implemented using the following two methods: private void writeObject(ObjectOutputStream oos) throws Exception: This method will be executed automatically by the jvm(also known as Callback Methods) at the time of serialization.

How many types of serialization that are commonly used?

There are three types of serialization in . Net : Binary Serialization, SOAP Serialization and XML Serialization.

Which type of objects can be serialized?

To serialize an object means to convert its state to a byte stream so way that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java.

Which method is used for object serialization?

To make a Java object serializable we implement the java. io. Serializable interface. The ObjectOutputStream class contains writeObject() method for serializing an Object.


1 Answers

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
    }
}

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);

Hope this helps.

like image 138
kenster Avatar answered Sep 30 '22 19:09

kenster