Share via


How to: Use a Custom Serializer with Azure In-Role Cache

Important

Microsoft recommends all new developments use Azure Redis Cache. For current documentation and guidance on choosing an Azure Cache offering, see Which Azure Cache offering is right for me?

Microsoft Azure Cache serializes objects before placing them into the cache and deserializes objects that are retrieved from the cache. Internally, the serialization is handled by the NetDataContractSerializer class. This topic explains how you can create and use a custom serialization class for use in your Azure applications that use Cache.

Tip

You can potentially gain performance benefits by optimizing the serialization of your known types.

To create the custom serialization class

  1. First prepare the development environment to use Cache.

  2. Create a class that derives from IDataCacheObjectSerializer.

  3. Implement the two methods in this interface: Serialize and Deserialize.

class MySerializer : IDataCacheObjectSerializer
{
    public object Deserialize(System.IO.Stream stream)
    {
            object returnObject = null;

            // Deserialize the System.IO.Stream 'stream' from
            // the cache and return the object.

            return returnObject;
    }

    public void Serialize(System.IO.Stream stream, object value)
    {
        // Serialize the object 'value' into the System.IO.Stream 'stream'
        // which will then be stored in the cache.
    }
}

To use the configuration file to specify the custom serializer

  1. Add the code for your custom serializer to your project, or add a reference to an assembly that implements the custom serializer.

  2. In the application configuration file, add a serializationProperties element within the dataCacheClient section.

  3. Within the serializationProperties element, assign the serializer attribute to "CustomSerializer". Assign the customSerializerType attribute to the class that implements the serializer.

The following example demonstrates how to use the application configuration file to specify a custom serializer named MyNamespace.MySerializer.

<dataCacheClient>
    <serializationProperties serializer="CustomSerializer" 
       customSerializerType="MyNamespace.MySerializer, MyAssembly" />
    <!-- Other dataCacheClient Elements, such as hosts -->
</dataCacheClient>

To use code to specify the custom serializer

  1. Add the code for your custom serializer to your project, or add a reference to an assembly that implements the custom serializer.

  2. Create a DataCacheSerializationProperties object, using the constructor to specify the DataCacheObjectSerializerType.CustomSerializer option and a new instance of your serializer class.

  3. Assign this object to the DataCacheFactoryConfiguration.SerializationProperties property, and use that DataCacheFactoryConfiguration object to configure a new DataCacheFactory.

The following example demonstrates how to use code to specify a custom serializer named MyNamespace.MySerializer.

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

Recommendations

If a custom serializer is used with a cache, all clients of that cache must use the same version of the custom serializer when accessing shared objects within that cache.