Str*_*tic 5 c# azure-service-fabric service-fabric-actor
有没有办法要求系统在启动时产生某些actor.
目前我Program.cs在演员注册后激活了我需要的演员集.
这工作正常,但我偶尔会得到一个,ReminderLoadInProgressException因为正在激活的演员需要注册一个提醒,但并非所有提醒都在尝试这样做时被加载.
是否有种子集群的标准方法?
不,你需要一些东西来激活你的演员.
Program.cs不是最好的地方,因为它的执行并不真正符合您服务的生命周期,正如您可能已经注意到的那样.
如果你需要在你的actor服务启动时激活一些actor,那么一个好的地方就是你的actor服务的RunAsync方法.您可以通过编写从中派生的服务来自定义您的actor服务,与编写有状态服务时一样:
编辑:确保你先调用并等待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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后注册您的actor服务,以便运行时知道使用它来托管您的actor实例:
static class Program
{
private static void Main()
{
ActorRuntime.RegisterActorAsync<MyActor>(
(context, actorType) => new MyActorService(context, actorType, () => new MyActor()))
.GetAwaiter().GetResult();
Thread.Sleep(Timeout.Infinite);
}
}
Run Code Online (Sandbox Code Playgroud)
只要确保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<IMyActor>(new ActorId(0), this.Context.ServiceName);
Run Code Online (Sandbox Code Playgroud)
否则,您将获得“服务不存在”异常:
System.Fabric.FabricServiceNotFoundException: Service does not exist.
---> System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80071BCD at
System.Fabric.Interop.NativeClient.IFabricServiceManagementClient4
.EndResolveServicePartition(IFabricAsyncOperationContext context)
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2348 次 |
| 最近记录: |