Skip to content

MessagePack serialization in Orleans

Use the MessagePack integration to serialize and copy types with existing MessagePack contracts in grain calls. Orleans wraps the MessagePack payload with its own wire metadata, including the runtime type.

MessagePack is useful when your application already uses types annotated with [MessagePackObject] and [Key]. For most Orleans applications, the default Orleans serializer provides .NET type fidelity, object identity preservation, and automatic serializer generation. Compare payload size and performance using your application’s types and workload.

Install the MessagePack serializer package

Section titled “Install the MessagePack serializer package”
Terminal window
dotnet add package Microsoft.Orleans.Serialization.MessagePack

Register the integration using AddMessagePackSerializer:

public static void ConfigureMessagePackBasic(string[] args)
{
var builder = Host.CreateApplicationBuilder(args);
builder.UseOrleans(siloBuilder =>
{
siloBuilder.UseLocalhostClustering();
siloBuilder.Services.AddSerializer(serializerBuilder => serializerBuilder.AddMessagePackSerializer(
isSerializable: type => type.Namespace?.StartsWith("MyApp.Messages", StringComparison.Ordinal) == true,
isCopyable: type => false,
messagePackSerializerOptions: null
));
});
}

The isSerializable and isCopyable delegates select additional types for serialization and deep copying, respectively. By default, the codec also recognizes types annotated with [MessagePackObject]. IsSerializableType and IsCopyableType provide explicit decisions for types not selected by those delegates; a null result uses attribute-based selection.

Apply matching registrations and MessagePack options on every silo and client that exchanges these types. See Authorize type-name resolution for shared type authorization settings.

Configure MessagePackCodecOptions to control type selection and MessagePack options:

PropertyTypeDefaultDescription
SerializerOptionsMessagePackSerializerOptionsMessagePackSerializerOptions.StandardThe MessagePack serializer options used for serialization and copying.
AllowDataContractAttributesboolfalseEnables selection of types marked with [DataContract].
IsSerializableTypeFunc<Type, bool?>nullDetermines whether a type is serialized by MessagePack.
IsCopyableTypeFunc<Type, bool?>nullDetermines whether a type is copied by MessagePack.
public static void ConfigureMessagePackWithOptions(string[] args)
{
var builder = Host.CreateApplicationBuilder(args);
builder.UseOrleans(siloBuilder =>
{
siloBuilder.UseLocalhostClustering();
siloBuilder.Services.AddSerializer(serializerBuilder => serializerBuilder.AddMessagePackSerializer(
isSerializable: type => type.GetCustomAttribute<MessagePackObjectAttribute>() != null,
isCopyable: type => false,
configureOptions: options => options.Configure(opts =>
{
opts.SerializerOptions = MessagePackSerializerOptions.Standard
.WithCompression(MessagePackCompression.Lz4BlockArray);
opts.AllowDataContractAttributes = true;
})
));
});
}

Use MessagePack attributes to define your contracts:

[MessagePackObject]
public class OrderMessage
{
[Key(0)]
public required string OrderId { get; set; }
[Key(1)]
public decimal Amount { get; set; }
[Key(2)]
public DateTime CreatedAt { get; set; }
[Key(3)]
public List<string> Items { get; set; } = [];
}

Use these types in grain interfaces:

public interface IOrderGrain : IGrainWithStringKey
{
Task<OrderMessage> GetOrder();
Task PlaceOrder(OrderMessage order);
}

MessagePack controls the representation and versioning of each delegated payload. Follow the MessagePack for C# documentation when evolving those contracts or configuring resolvers and security.