akka.net
akka.net copied to clipboard
Problem with serializing messages with exceptions
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 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
Btw. this is bug related specifically to Json.NET - when using Wire serializer, everything works correctly. (see how to setup a Wire serializer).
@Horusiath your example works fine, without SurrogateConverter in JsonSerializerSettings.
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.
Partially fixed in https://github.com/akkadotnet/akka.net/pull/2925 only for system messages.
@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.
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.