简体   繁体   中英

Send non-DataContract class to Azure Service Fabric Actor

The Service Fabric requires [DataContract] and [DataMember] attributes for all the classes that are used as input parameters for the Actor services.

Is there a way to override this default?

In our project we heavily use the read-only message classes, which have read-only properties and constructors. The serialization is handled by Newtonsoft JSON serializer, which works just great. Now, I want to send this messages in Service Fabric, so I need a way to override the default WCF-like serialization to something like JSON serializer.

I don't think this is possible at the minute unfortunately. From this article,

"Reliable Collections allow the serializer to be overridden, but Reliable Actors currently do not."

So it might be worth mapping to new classes.

I was able to make a workaround by implementing the serialization myself. Basically, this is how it works:

  1. Define an actor (+ contract) which accepts and returns byte[] + type name, eg

     Task<byte[]> DoWork(string typeName, byte[] message); 
  2. Deserialize byte[] to an instance of the specified type by using custom serializer (I used Newtonsoft JSON converter).

  3. On the sending side, serialize object to byte[] using the same serializer.

I defined some base classes to wrap that, so I don't have to repeat it for every actor / client.

This feels a bit hacky though. Would be nice to get some opinion from the Service Fabric team. Would be even nicer to get the native extensibility point from the platform itself. DataContract feels a bit archaic, it's not used anywhere in our project anymore.

This approach is described in more details in my blog post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM