Service Fabric在启动时生成actor

有没有办法要求系统在启动时产生某些演员。

目前我在演员注册后激活了Program.cs中我需要的演员集。

这工作正常,但我偶尔会得到一个ReminderLoadInProgressException因为正在激活的actor需要注册一个提醒,但并非所有提醒都在它尝试这样做时被加载。

是否有种子集群的标准方法?

不,你需要一些东西来激活你的演员。

Program.cs不是最好的地方,因为它的执行并不真正符合您服务的生命周期,正如您可能已经注意到的那样。

如果你需要在你的actor服务启动时激活一些actor,那么一个好的地方就是你的actor服务的RunAsync方法。 您可以通过编写从中派生的服务来自定义您的actor服务,就像编写有状态服务时一样:

编辑:确保你先打电话等待base.RunAsync()!

 class MyActorService : ActorService { public MyActorService(StatefulServiceContext context, ActorTypeInformation typeInfo, Func newActor) : base(context, typeInfo, newActor) { } protected async override Task RunAsync(CancellationToken cancellationToken) { await base.RunAsync(cancellationToken); var proxy = ActorProxy.Create(new ActorId(0)); await proxy.StartDoingStuff(); } } 

然后注册您的actor服务,以便运行时知道使用它来托管您的actor实例:

 static class Program { private static void Main() { ActorRuntime.RegisterActorAsync( (context, actorType) => new MyActorService(context, actorType, () => new MyActor())) .GetAwaiter().GetResult(); Thread.Sleep(Timeout.Infinite); } } 

只要确保actor方法是幂等的,因为这将在每次启动actor服务的主副本时执行(在故障转移,资源平衡,应用程序升级等之后),但好处是它不会在你的执行之前执行actor服务已准备就绪,它将始终在服务启动时执行。

有关如何使用actor服务的更多信息,请参阅此处: https : //azure.microsoft.com/en-us/documentation/articles/service-fabric-reliable-actors-platform/

无法发表评论所以我在这里发表评论:

为了完整起见:如果您的ActorService子类没有默认名称(来自ServiceManifest.xml – 例如您动态生成服务实例),则必须使用双参数ActorProxy.Create重载,如下所示:

 var actor = ActorProxy.Create(new ActorId(0), this.Context.ServiceName); 

否则你会得到“服务不存在”的例外:

 System.Fabric.FabricServiceNotFoundException: Service does not exist. ---> System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80071BCD at System.Fabric.Interop.NativeClient.IFabricServiceManagementClient4 .EndResolveServicePartition(IFabricAsyncOperationContext context) ...