Skip to content

Error deserializing messages when using custom Akka serializers #215

@vadymberkut

Description

@vadymberkut

Version Information
Akka 1.4.31

Akka.Quartz.Actor 1.4.31`

Describe the bug
When using Akka custom message serializer and QuartzPersistentActor it fails to deserialize message from binary in QuartzPersistentJob.Execute, as it searches for serializer for typeof(object) which is Akka.Serialization.NewtonSoftJsonSerializer by default. However, initially, the message was serialized with a custom serializer in QuartzPersistentJob.CreateBuilderWithData.

I have custom serializers defined in Akka to serialize messages using protobuf. I inherit my serializers from SerializerWithStringManifest and use manifest that is based on the message type. When doing

var message = sys.Serialization.FindSerializerForType(typeof(object)).FromBinary(messageBytes, typeof(object));

in QuartzPersistentJob.Execute, it can't find proper serializer for type typeof(object) and returns Akka.Serialization.NewtonSoftJsonSerializer (default) which can't deserialize, because bytes are not json bytes, but protobuf bytes.

I tried to implement a custom serializer to handle deserializaion of typeof(object), but there is no way to figure out which exact type the bytes belong to, when the second argument type is always object type:

public class ObjectSerializer : Akka.Serialization.Serializer
{
  // ....
  public override object FromBinary(byte[] bytes, Type type) 
  {
     // typeof(type) == typeof(object)
     // how to figure out which class is encoded in bytes?
  }
}

FromBinary is called from here:
image

To Reproduce
Steps to reproduce the behavior:

  1. Register a custom Akka binary serializer that is not JSON serializer.
  2. Enable Quartz logs.
  3. Create QuartzPersistentActor and schedule a job via it with a message that is serialized by serializer from 1..
  4. When job is triggered you shoud see an error like this
    image

Expected behavior
Pass correct Type type to FromBinary when executing a job, not typeof(object). Or introduce any other solution that will fix the issue.

Users of Akka.Quartz.Actor must be able to use any custom serializers.

Actual behavior
Serialized messages can't be deserialized back.

Environment
Windows 10, .NET 5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions