简体   繁体   English

将非DataContract类发送到Azure Service Fabric Actor

[英]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. Service Fabric需要[DataContract][DataMember]属性用于所有用作Actor服务的输入参数的类。

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. 序列化由Newtonsoft JSON序列化程序处理,它运行得很好。 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. 现在,我想在Service Fabric中发送此消息,因此我需要一种方法来覆盖类似JSON序列化程序的默认类WCF序列化。

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." “Reliable Collections允许覆盖序列化器,但Reliable Actors目前不会。”

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 定义一个接受并返回byte[] +类型名称的actor(+ contract),例如

     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). 使用自定义序列化程序将byte[]反序列化为指定类型的实例(我使用的是Newtonsoft JSON转换器)。

  3. On the sending side, serialize object to byte[] using the same serializer. 在发送端,使用相同的序列化器将对象序列化为byte[]

I defined some base classes to wrap that, so I don't have to repeat it for every actor / client. 我定义了一些基类来包装它,所以我不必为每个actor / client重复它。

This feels a bit hacky though. 这感觉有点哈哈。 Would be nice to get some opinion from the Service Fabric team. 很高兴从Service Fabric团队获得一些意见。 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. DataContract感觉有点陈旧,它不再用于我们项目的任何地方。

This approach is described in more details in my blog post . 在我的博客文章中更详细地描述了这种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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