不,你需要一些东西来激活你的演员。
Program.cs 不是执行此操作的最佳位置,因为您可能已经注意到,它的执行与您的服务的生命周期并不相符。
如果您需要在您的演员服务启动时激活一些演员,那么执行此操作的好地方是您的演员服务的 RunAsync 方法。您可以通过编写派生自它的服务来自定义您的参与者服务,就像您编写有状态服务时所做的一样:
编辑:确保首先调用并等待 base.RunAsync()!
class MyActorService : ActorService
{
public MyActorService(StatefulServiceContext context, ActorTypeInformation typeInfo, Func<ActorBase> newActor)
: base(context, typeInfo, newActor)
{ }
protected async override Task RunAsync(CancellationToken cancellationToken)
{
await base.RunAsync(cancellationToken);
var proxy = ActorProxy.Create<IMyActor>(new ActorId(0));
await proxy.StartDoingStuff();
}
}
然后注册您的演员服务,以便运行时知道使用它来托管您的演员实例:
static class Program
{
private static void Main()
{
ActorRuntime.RegisterActorAsync<MyActor>(
(context, actorType) => new MyActorService(context, actorType, () => new MyActor()))
.GetAwaiter().GetResult();
Thread.Sleep(Timeout.Infinite);
}
}
只要确保actor方法是幂等的,因为每次启动actor服务的主副本时都会执行(在故障转移、资源平衡、应用程序升级等之后),但好的是它不会在你的actor服务准备好之前执行,它总是会在服务启动时执行。
有关如何使用演员服务的更多信息,请参见此处:https://azure.microsoft.com/en-us/documentation/articles/service-fabric-reliable-actors-platform/