我应该如何将 Service Fabric Actor 关联在一起?

How should I relate Service Fabric Actors together?

我有许多代表单个物理对象(IoT 设备)的 Actor。

在审查我们的代码库时,我们有时会将现有的 ActorReference 传递给其他 Actor,有时我们会创建一个新的代理对象。

我原以为传入现有 Actor 引用的性能更高,但我担心副作用,因此创建新的代理对象似乎风险较低。

每种方法的优缺点是什么?在决定使用哪种方法时我应该考虑什么?

通过研究反编译的源代码,在我看来使用 ActorReference.Bind 和 ActorProxy.Create 几乎相等:

public object Bind(Type actorInterfaceType)
{
    return ActorProxy.DefaultProxyFactory.CreateActorProxy(actorInterfaceType, this.ServiceUri, this.ActorId, this.ListenerName);
}

public static TActorInterface Create<TActorInterface>(ActorId actorId, string applicationName = null, string serviceName = null, string listenerName = null) where TActorInterface : IActor
{
    return ActorProxy.DefaultProxyFactory.CreateActorProxy<TActorInterface>(actorId, applicationName, serviceName, listenerName);
}

所以在可靠性和性能上都没有差异。

ActorReference支持序列化所以看起来更适合在actors之间传递。

传递 ActorReference 是更好的推荐方法,因为它允许您在接收端使用自定义 ActorProxyFactory 创建 ActorProxy。如果 actor 作为接口传递,反序列化过程使用默认的 actor 代理工厂将引用绑定到代理并将代理对象提供给接收方法。