microsoft / service-fabric-services-and-actors-dotnet

Reliable Services and Reliable Actors are Service Fabric application frameworks for building highly-scalable distributed cloud applications.
Other
269 stars 113 forks source link

Serialization of .NET 6.0 DateOnly with actors #314

Open RemcoBlok opened 2 years ago

RemcoBlok commented 2 years ago

Service Fabric using the DataContractSerializer does not currently support serialization and deserialization of .NET 6.0 DateOnly type used in dto's, parameters or return values. Can you advise how I can add this support? I'm not sure how to do this with the DataContractSerializer.

I know how to add this support using custom converters when using a json serializer (either Newtonsoft or System.Text.Json). So I looked at the example here Use custom serialization with a remoting wrapped message for how I might replace the DataContractSerializer with a json serializer.

A couple of issues:

I use v2 remoting but not the interface compatible v2.1 as I cannot use wrapped messages. I tried using v2.1 with wrapped messages but it does not work for some reason. Perhaps it does not work because I already customize the client and listener so that I can add custom headers containing context to the transport .

I want to use System.Text.Json and not Newtonsoft. How should I implement polymorphic deserialization which needs to be implemented using a custom converter as discussed here Support polymorphic deserialization, when I'm not directly (de)serializing my dto's / parameters / return values, but I'm (de)serializing a IServiceRemotingRequestMessageBody or IServiceRemotingResponseMessageBody. I will need to serialize the type information.

Specifically when using actors I may need to do something similar to the ActorDataContractSurrogate and ActorReference used by ActorRemotingDataContractSerializationProvider when using a json serializer

RemcoBlok commented 2 years ago

I have a working solution now that appears to do the job. I created a repo here: https://github.com/RemcoBlok/ServiceFabric.Remoting.Json

It can serialize and deserialize DateOnly types using System.Text.Json It can also do polymorphic deserialization. It uses non-wrapped messages. So using remoting v2, not remoting v2.1.

I added a converter for IActor using an ActorReference similar to the ActorDataContractSurrogate, but it does not get used. I like to understand if it is needed at all.

I still need to perform loadtesting of our system with the new serialization to see if how it performs under load. The standard DataContractSerializer seems to use IPooledBuffer instead of ArraySegment. I use ArraySegment. My payloads are small so I'm not sure if I need to look into IPooledBuffer. The loadtesting will tell.

It would be really good if the ServiceFabric team could review this and advise where it might need improving.

pakunapamsft commented 2 years ago

The arguments and return types of the remoting methods has to be data contract serializable(https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-communication-remoting#set-up-remoting-on-a-service). It seems to me that DateOnly is not Data contract serializable.