Exporteren (0) Afdrukken
Alles uitvouwen
EN
Deze inhoud is niet beschikbaar in uw taal, maar wel in het Engels.

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

Updated: February 13, 2015

noteNote
For guidance on choosing the right Azure Cache offering for your application, 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.

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

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

  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>

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

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.

Weergeven:
© 2015 Microsoft