akka.net icon indicating copy to clipboard operation
akka.net copied to clipboard

Problem with serializing messages with exceptions

Open Horusiath opened this issue 9 years ago • 6 comments

Default serializer is not able to properly serialize/deserialize messages with exceptions.

.NET version: 4.6.1 Akka.NET version: 1.0.7 Newtonsoft.Json version: 7.0.1

Example

public sealed class Message
{
    public readonly long Id;
    public readonly Exception Reason;

    public Message(long id, Exception reason)
    {
        Id = id;
        Reason = reason;
    }
}

using (var system = ActorSystem.Create("sys"))
{
    var message = new Message(1, new Exception("custom"));
    var serializer = system.Serialization.FindSerializerFor(message);
    var binary = serializer.ToBinary(message);
    var msg = serializer.FromBinary(binary, null); // Error: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JValue'.
}

Horusiath avatar Apr 15 '16 07:04 Horusiath

@Horusiath I've also noticed such behavior. Here is some related issues

https://github.com/JamesNK/Newtonsoft.Json/issues/780

https://github.com/akkadotnet/akka.net/issues/1409#issuecomment-169845022

akoshelev avatar Apr 15 '16 19:04 akoshelev

Btw. this is bug related specifically to Json.NET - when using Wire serializer, everything works correctly. (see how to setup a Wire serializer).

Horusiath avatar Jul 17 '16 11:07 Horusiath

@Horusiath your example works fine, without SurrogateConverter in JsonSerializerSettings.

alexvaluyskiy avatar Jul 18 '16 06:07 alexvaluyskiy

Scala version consists a separate serializer for Throwable https://github.com/akka/akka/blob/master/akka-remote/src/main/scala/akka/remote/serialization/ThrowableSupport.scala

We could do the same, but it would be a bit harder. We could use a serializer based on ISerializable on .NET and reflection based serializer on NetCore 1.x

Or we could create a separate serializer for ActorInitializationException. It it only one system exception, which could be serialized.

alexvaluyskiy avatar Apr 23 '17 14:04 alexvaluyskiy

Partially fixed in https://github.com/akkadotnet/akka.net/pull/2925 only for system messages.

alexvaluyskiy avatar Aug 07 '17 14:08 alexvaluyskiy

@Arkatufus can you take on writing a spec for this? There are other open issues related to this one too:

  • https://github.com/akkadotnet/akka.net/issues/3903
  • https://github.com/akkadotnet/akka.net/issues/2400

There are proposals and spec ideas from other users on those issues - need to consider their designs and put something together in writing on this issue before we move forward with a PR.

Aaronontheweb avatar Aug 19 '22 14:08 Aaronontheweb

Per https://github.com/akkadotnet/akka.net/issues/3903#issuecomment-1341858658, looks like we've had built-in support for this for some time but haven't been using it consistently outside of remote deployment scenarios. Going to close this issue and focus on #3903 are the primary implementation point for this.

Aaronontheweb avatar Dec 08 '22 01:12 Aaronontheweb