How to Use Dynamic Compact Serialization

Why Compact Serialization:

Itís a process of producing a compact form of serialized object for performance improvement which results in faster network trips. As name illustrates, compact serialized file is compact in form of data and process. The idea behind compact serialization is that every 'known type' (a known type is a type that is registered with the framework) is assigned a unique 2-bytes type handle by the framework, which lets the deserializer uniquely identify the 'known types'.

For example, suppose the framework assign a handle 0 to System.Int32. Then the serializer writes the 2-byte handle followed by 4 bytes of object value to the stream. Deserializer reads the handle, figures out the object type, and then creates and populates the object with its value. On other hand the native serialization writes the complete type information with object data. NCache sophisticatedly over comes these issues of native sterilization in the following ways:

  • Here serialized object does not contain complete type information and assemblies. It only contains an object value and a unique 2-byte handle. This unique 2-byte handle is used as a reference to uniquely identify the type details while deserializing an object. In this way the size of the object reduces
  • Compact serialization also avoids the reflection which also reduces the overhead
  • The above two features reduces the IO operations
  • As the object size decreases, it automatically faster the network trips and eventually the cost decrease
  • Small object size and fast network traffic increases the application performance

For using Compact Serialization, you need to define a default public constructor (parameter less) in a compact serializable type and register that type with NCache Manager. NCache now supports Custom Generic Types. All generic types with any number of arguments can be serialized through compact serialization. If a client doesn't want to add generic types and its parameters though UI, then he has the option to add them through IGenericTypes class.

How to Use Dynamic Compact Serialization:

To use Compact Serializable types with NCache Configure Cache with NCache Manager and then register your compact serializable types with NCache.

Step 1: Registering Compact Serializable Types:

You can register types one by one through 'Add Types' button. Perform the following steps to register your types with configured cache:

  • Stop the cache if it is running
  • Click on the 'Compact Serialization' tab which will open up the following screen


  • Click 'Add Types' button. A 'Select Compact Classes' dialog will appear
  • Browse for the assembly file that contains the compact types by clicking the 'Browse' button as shown in the figure
  • Select the classes you want to register with the cache from the loaded classes list
  • Press 'Add Class' button to add the classes in the selected classes list
  • Click 'Ok' button


  • Newly added types will start appearing in the list box under the 'Compact Serialization' tab having Compact Type and Platform information
  • 'Remove' button will delete the selected type from the registered types list


  • This shows all the added types. Now arguments can be added with generic types by selecting specific type and then clicking on 'Add <T>' button

Step 2: Registering Generic Compact Types Through Generic Type Handler

This is the other way for registering generic compact types through "Generic Type Handler". With Generic Type Handler user can add all the Generic types at once by implementing our interface. This saves time and effort. Here is a sample code for this.

public class CustomGenericType <T,K,V>         {           }  

        public class SetGenericsIGenericTypeProvider


           Type[] types = new Type[6];                     #region IGenericTypeProvider Members                Type[] IGenericTypeProvider.GetGenericTypes()             {                types[0]=typeof(Dictionary<stringCustomer>);                  types[1]=typeof(List<int>);                   types[2]=typeof(CustomGenericType<EmployeeProductCustomer>);               types[3]=typeof(CustomGenericType<EmployeeProductint>);               types[4]=typeof(CustomGenericType<Employeestringbyte>);                 types[5]=typeof(CustomGenericType<stringCustomerbool>);                   return types;               }             #endregion             }

For registering types follow these steps.

  • Click 'Generic Type Handler' button. A 'Select Compact Classes' dialog box will appear
  • Browse for the assembly file that implements this interface and contains compact types by clicking the 'Browse' button as shown in the figure


  • Click 'OK' button. All the added types will be listed as shown below.


What to Do Next?