有点像但不是真的。您可以在同一个角色中拥有多种演员类型应用。看起来它们在 Visual Studio 中位于同一个服务中,但实际上它们是作为单独的服务部署的。如果你愿意的话,请耐心等待我一分钟。
所以你可以像这样注册多个演员:
internal static class Program
{
private static void Main()
{
ActorRuntime.RegisterActorAsync<Actor1>().GetAwaiter().GetResult();
ActorRuntime.RegisterActorAsync<Actor2>().GetAwaiter().GetResult();
Thread.Sleep(Timeout.Infinite);
}
}
太好了,我有多种演员类型。这行得通,你就可以这样做。
但你想知道how有用!嗯,这只是一个简化版本:
internal static class Program
{
private static void Main()
{
ActorRuntime.RegisterActorAsync<Actor1>(
(context, actorType) => new ActorService(context, actorType, () => new Actor1())).GetAwaiter().GetResult();
ActorRuntime.RegisterActorAsync<Actor2>(
(context, actorType) => new ActorService(context, actorType, () => new Actor2())).GetAwaiter().GetResult();
Thread.Sleep(Timeout.Infinite);
}
}
这更能说明实际发生的情况,因为你在这里看到我现在有两项服务。发生什么了?
秘密就在 ActorRuntime 中。它比 ServiceRuntime(您通常在其中注册 Reliable Services)多做一些工作。 Actor 框架构建过程会为您执行一些魔法,在您的应用程序内配置服务类型和默认服务实例对于每种演员类型。您可以在 ApplicationManifest.xml 中看到这一点,其中构建工具为您设置默认服务:
<DefaultServices>
<Service Name="Actor1ActorService" GeneratedIdRef="3262c188-3eee-44c5-9d1e-d2c2a2685f89|Persisted">
<StatefulService ServiceTypeName="Actor1ActorServiceType" TargetReplicaSetSize="[Actor1ActorService_TargetReplicaSetSize]" MinReplicaSetSize="[Actor1ActorService_MinReplicaSetSize]">
<UniformInt64Partition PartitionCount="[Actor1ActorService_PartitionCount]" LowKey="-9223372036854775808" HighKey="9223372036854775807" />
</StatefulService>
</Service>
<Service Name="Actor2ActorService" GeneratedIdRef="1bc66d2c-0479-4bb2-a9aa-3254030506f1|Persisted">
<StatefulService ServiceTypeName="Actor2ActorServiceType" TargetReplicaSetSize="[Actor2ActorService_TargetReplicaSetSize]" MinReplicaSetSize="[Actor2ActorService_MinReplicaSetSize]">
<UniformInt64Partition PartitionCount="[Actor2ActorService_PartitionCount]" LowKey="-9223372036854775808" HighKey="9223372036854775807" />
</StatefulService>
</Service>
例如,如果我采用上面定义的两种参与者类型并部署该应用程序,则结果如下:
这些实际上是应用程序中单独的服务实例,每个实例都有不同的服务类型,所有这些都是自动为您生成的:
当然,因为它们是不同的服务实例,所以它们将像您通常期望的那样分布在集群中:
我会去更新那个文档。