Share via


IReliableStateManager.TryAddStateSerializer<T> Method

Definition

Registers a custom serializer for all reliable collections.

public bool TryAddStateSerializer<T> (Microsoft.ServiceFabric.Data.IStateSerializer<T> stateSerializer);
abstract member TryAddStateSerializer : Microsoft.ServiceFabric.Data.IStateSerializer<'T> -> bool
Public Function TryAddStateSerializer(Of T) (stateSerializer As IStateSerializer(Of T)) As Boolean

Type Parameters

T

Type that will be serialized and deserialized.

Parameters

stateSerializer
IStateSerializer<T>

The state serializer to be added.

Returns

True if the custom serializer was added. False if a custom serializer for the given type already exists.

Remarks

When a reliable collection needs to serialize an object, it asks the state manager for a serializer for the given type. The state manager will first check if there is a custom serializer registered for the input type. If not, it will check if one of the built-in serializers can serialize the type. The state manager has built-in serializers for the following types: guid, bool, byte, sbyte, char, decimal, double, float, int, uint, long, ulong, short, ushort and string. If not, it will use DataContractSerializer.

Serializers must be infinitely forwards and backwards compatible. For the types that are using built-in serializers, Service Fabric ensures forwards and backwards compatibility. However, if a custom serializer is added for a type with a built-in serializer, the custom serializer must be compatible with the built-in serialization format for that type.

This method should be called from the constructor of the Stateful Service. This ensures that the Reliable Collections have the necessary serializers before recovery of the persisted state begins.

Applies to