Azure Service Fabric StatelessService 不调用 OnCloseAsync

Azure Service Fabric StatelessService does not call OnCloseAsync

我有一个简单的 StatelessService,我想知道它何时关闭,以便我可以执行一些快速清理。但它似乎从不调用 OnCloseAsync。

当服务为 运行 并且我通过 Service Fabric Explorer 在 运行 节点上使用 'Restart' 命令时,它会删除服务并重新启动节点。但它从不调用 OnCloseAsync 覆盖,即使它有意关闭。

它也不向传递给 RunAsync 方法的 cancellationToken 发出信号。所以没有迹象表明该服务正在关闭。是否存在调用 OnCloseAsync 的任何情况,因为目前我看不出有什么意义。

我想知道发出重启命令背后的原因,您期望什么行为? 它确实解释了您看到的行为。来自 the docs(请记住,重新启动只是停止和开始的组合)

Stopping a node puts it into a stopped state where it is not a member of the cluster and cannot host services, thus simulating a down node. This is useful for injecting faults into the system to test your application.

现在,如果我们看一下 the lifecycle,我们会读到:

After CloseAsync() finishes on each listener and RunAsync() also finishes, the service's StatelessService.OnCloseAsync() method is called, if present. OnCloseAsync is called when the stateless service instance is going to be gracefully shut down.

所以,基本问题是您的服务没有正常关闭。重新启动命令会终止进程,并且不会发出取消命令。