This CompressedJsonSerializer
Akka.NET serializer is a compressed version of the built-in Akka.Serialization.NewtonSoftJsonSerializer
.
You can add this serializer by using the Akka.Hosting
extension method:
using var host = new HostBuilder()
.ConfigureServices((context, services) =>
{
services.AddAkka("compressed", (builder, provider) =>
{
builder.WithCompressedJsonSerializer();
});
}).Build();
await host.RunAsync();
To use this serializer, add these settings to your HOCON configuration:
akka.actor {
serializers {
json-gzip = "Akka.Serialization.Compressed.Json.CompressedJsonSerializer, Akka.Serialization.Compressed.Json"
}
serialization-bindings {
"Akka.Serialization.Compressed.Json.IShouldCompress, Akka.Serialization.Compressed.Json" = json-gzip
}
}
The Akka.Hosting
extension and HOCON configuration above binds the marker IShouldCompress
interface to the serializer. To start compressing your data model, make your classes inherit from this interface.
public sealed class MyDataModel: IShouldCompress
{
// ...
}
To bind your your data model to the serializer using Akka.Hosting
, you can use the extension method below:
public static class DataClassOne
{ }
public static class DataClassTwo
{ }
using var host = new HostBuilder()
.ConfigureServices((context, services) =>
{
services.AddAkka("compressed", (builder, provider) =>
{
builder.WithCompressedJsonSerializer(
typeof(DataClassOne),
typeof(DataClassTwo);
});
}).Build();
await host.RunAsync();
You can bind your classes to this serializer by registering them in the HOCON configuration:
akka.actor.serialization-bindings {
"MyAssembly.DataClassOne, MyAssembly" = json-gzip
"MyAssembly.DataClassTwo, MyAssembly" = json-gzip
}