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.
When to use MessagePack
Section titled “When to use MessagePack”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”dotnet add package Microsoft.Orleans.Serialization.MessagePackConfigure MessagePack serialization
Section titled “Configure MessagePack serialization”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.
MessagePackCodecOptions
Section titled “MessagePackCodecOptions”Configure MessagePackCodecOptions to control type selection and MessagePack options:
| Property | Type | Default | Description |
|---|---|---|---|
SerializerOptions | MessagePackSerializerOptions | MessagePackSerializerOptions.Standard | The MessagePack serializer options used for serialization and copying. |
AllowDataContractAttributes | bool | false | Enables selection of types marked with [DataContract]. |
IsSerializableType | Func<Type, bool?> | null | Determines whether a type is serialized by MessagePack. |
IsCopyableType | Func<Type, bool?> | null | Determines whether a type is copied by MessagePack. |
Example: Configure with options
Section titled “Example: Configure with options”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; }) )); });}Define MessagePack types
Section titled “Define MessagePack types”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.
