NamedCacheSetCustomSerialization Method

ScaleOut Software NamedCache API
Establishes custom serialize/deserialize callbacks for objects in this NamedCache.

Namespace:  Soss.Client
Assembly:  soss_namedcache (in soss_namedcache.dll) Version:

public void SetCustomSerialization(
	Serializer serializer,
	Deserializer deserializer


Type: Soss.ClientSerializer
The custom serializer to use.
Type: Soss.ClientDeserializer
The custom deserializer to use.

ArgumentNullException If one of serializer or deserializer is null and the other is non-null.

If serializer and deserializer are non-null, this NamedCache instance will use them whenever serializing or deserializing objects to the ScaleOut StateServer. If serializer and deserializer are both null, this NamedCache instance will use the default ScaleOut serialization mechanism. If one of serializer or deserializer are non-null and the other null, an ArgumentNullException is thrown.

Compressing Objects Using Custom Serialization
using System;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
using Soss.Client;

/// <summary>
/// Illustrates how to plug custom serialization/deserialization
/// routines into ScaleOut StateServer's NamedCache API. In this case, the 
/// custom routines use the System.IO.Compression.DeflateStream class to perform
/// compression on an object before having it sent to ScaleOut StateServer
/// for storage. The custom methods are registered as callbacks through a call
/// to NamedCache.SetCustomSerialization.
/// </summary>
class CustomSerialization
    static void Main(string[] args)
        const int OBJ_SIZE_BYTES = 100000;

        // Create a large DataSet to store in ScaleOut StateServer:
        DataSet objectToStore = CreateBigDataSet(OBJ_SIZE_BYTES);

        // Use two named caches, one that uses the NamedCache's standard serialization and one
        // that uses our custom serialization methods that perform compression:
        NamedCache normalCache = CacheFactory.GetCache("normal cache");
        NamedCache compressedCache = CacheFactory.GetCache("compressed cache");
        compressedCache.SetCustomSerialization(SerializeAndDeflate, InflateAndDeserialize);

        // Store the object in SOSS and then display its size in the store:
        normalCache["my dataset"] = objectToStore;
        ExtendedObjectMetadata meta1 = normalCache.GetMetadata("my dataset", false);
        Console.WriteLine("Serialized size: " + meta1.ObjectSize.ToString());

        // Store the object in SOSS and then display its compressed size in the store:
        compressedCache["my dataset"] = objectToStore;
        ExtendedObjectMetadata meta2 = compressedCache.GetMetadata("my dataset", false);
        Console.WriteLine("Serialized/Compressed size: " + meta2.ObjectSize.ToString());

        // Confirm that we can retrieve the compressed object:
        DataSet obj1 = compressedCache["my dataset"] as DataSet;
        Console.WriteLine("Object retrieved successfully.");
        Console.WriteLine("Hit ENTER to continue...");

        // Clean up.
        normalCache["my dataset"] = null;
        compressedCache["my dataset"] = null;

    /// <summary>
    /// Custom serialization callback for use by the NamedCache. Serializes
    /// and then compresses and object for storage in ScaleOut StateServer.
    /// </summary>
    /// <param name="stream">Stream to write compressed bytes to.</param>
    /// <param name="obj">Object to serialize and compress.</param>
    private static void SerializeAndDeflate(Stream stream, Object obj)
        MemoryStream serializedStream = new MemoryStream();

        // Serialize the object:
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(serializedStream, obj);

        // Perform compression.
        // Configure the compression stream to stay open until we explicitly close it.
        DeflateStream gzCompressed = new DeflateStream(stream, CompressionMode.Compress, true);

        gzCompressed.Write(serializedStream.GetBuffer(), 0, (int)serializedStream.Length);

    /// <summary>
    /// Custom deserialization callback for use by the NamedCache. Decompresses
    /// and then deserializes the object that is stored in ScaleOut StatServer.
    /// </summary>
    /// <param name="stream">Stream containing the bytes returned from ScaleOut StateServer.</param>
    /// <returns>The original object stored in ScaleOut StateServer.</returns>
    private static object InflateAndDeserialize(Stream stream)
        // Inflate the bytes and then deserialize.
        // Leave the stream open until we close it explicitly.
        DeflateStream gzUncompressed = new DeflateStream(stream, CompressionMode.Decompress, true);

        BinaryFormatter formatter = new BinaryFormatter();
        object retVal = formatter.Deserialize(gzUncompressed);

        return retVal;

    /// <summary>
    /// Creates a relatively complex object that can be put into ScaleOut StateServer.
    /// </summary>
    /// <param name="roughSize">Approximate desired size of the DataSet (in bytes) when serialized with the BinaryFormatter.</param>
    /// <returns>A new DataSet.</returns>
    private static DataSet CreateBigDataSet(int roughSize)
        DataSet ds = new DataSet();
        DataTable t = ds.Tables.Add("table");
        t.Columns.Add("one", typeof(string));
        t.Columns.Add("two", typeof(int));
        t.Columns.Add("three", typeof(string));

        // Each row in this dataset takes up roughly 138 bytes after serialization, 
        // (not very accurate if OBJ_SIZE_BYTES < 10,000 bytes):
        int rowCount = roughSize / 138;
        for (int i = 0; i < rowCount; i++)
            t.Rows.Add(new object[] { i.ToString(), i, i.ToString() });
        return ds;
See Also