有关 Service Fabric Reliable Actors 类型序列化的说明Notes on Service Fabric Reliable Actors type serialization

所有方法的参数、执行组件接口中每个方法返回的任务的结果类型和执行组件的状态管理器中存储的对象都必须是数据协定可序列化The arguments of all methods, result types of the tasks returned by each method in an actor interface, and objects stored in an actor's state manager must be data contract serializable. 这同样适用于执行组件事件接口中定义的方法的参数。This also applies to the arguments of the methods defined in actor event interfaces. (执行组件事件接口方法始终返回 void。)(Actor event interface methods always return void.)

自定义数据类型Custom data types

在此示例中,以下执行组件接口定义了一个返回自定义数据类型(名为 VoicemailBox)的方法:In this example, the following actor interface defines a method that returns a custom data type called VoicemailBox:

public interface IVoiceMailBoxActor : IActor
{
    Task<VoicemailBox> GetMailBoxAsync();
}
public interface VoiceMailBoxActor extends Actor
{
    CompletableFuture<VoicemailBox> getMailBoxAsync();
}

此接口由使用状态管理器来存储 VoicemailBox 对象的执行组件实现:The interface is implemented by an actor that uses the state manager to store a VoicemailBox object:

[StatePersistence(StatePersistence.Persisted)]
public class VoiceMailBoxActor : Actor, IVoicemailBoxActor
{
    public VoiceMailBoxActor(ActorService actorService, ActorId actorId)
        : base(actorService, actorId)
    {
    }

    public Task<VoicemailBox> GetMailboxAsync()
    {
        return this.StateManager.GetStateAsync<VoicemailBox>("Mailbox");
    }
}

@StatePersistenceAttribute(statePersistence = StatePersistence.Persisted)
public class VoiceMailBoxActorImpl extends FabricActor implements VoicemailBoxActor
{
    public VoiceMailBoxActorImpl(ActorService actorService, ActorId actorId)
    {
         super(actorService, actorId);
    }

    public CompletableFuture<VoicemailBox> getMailBoxAsync()
    {
         return this.stateManager().getStateAsync("Mailbox");
    }
}

在此示例中,如果 VoicemailBox 对象出现以下情况,将序列化该对象:In this example, the VoicemailBox object is serialized when:

  • 在执行组件实例和调用方之间传输该对象。The object is transmitted between an actor instance and a caller.
  • 该对象保存在状态管理器中,即持久保存在磁盘中,并且已复制到其他节点。The object is saved in the state manager where it is persisted to disk and replicated to other nodes.

Reliable Actor 框架使用 DataContract 序列化。The Reliable Actor framework uses DataContract serialization. 因此,自定义数据对象及其成员必须分别使用 DataContractDataMember 属性进行批注。Therefore, the custom data objects and their members must be annotated with the DataContract and DataMember attributes, respectively.

[DataContract]
public class Voicemail
{
    [DataMember]
    public Guid Id { get; set; }

    [DataMember]
    public string Message { get; set; }

    [DataMember]
    public DateTime ReceivedAt { get; set; }
}
public class Voicemail implements Serializable
{
    private static final long serialVersionUID = 42L;

    private UUID id;                    //getUUID() and setUUID()

    private String message;             //getMessage() and setMessage()

    private GregorianCalendar receivedAt; //getReceivedAt() and setReceivedAt()
}
[DataContract]
public class VoicemailBox
{
    public VoicemailBox()
    {
        this.MessageList = new List<Voicemail>();
    }

    [DataMember]
    public List<Voicemail> MessageList { get; set; }

    [DataMember]
    public string Greeting { get; set; }
}
public class VoicemailBox implements Serializable
{
    static final long serialVersionUID = 42L;

    public VoicemailBox()
    {
        this.messageList = new ArrayList<Voicemail>();
    }

    private List<Voicemail> messageList;   //getMessageList() and setMessageList()

    private String greeting;               //getGreeting() and setGreeting()
}

后续步骤Next steps