我想RunAsync
方法需要CancellationToken
作为一个很好的参数。 不幸的是,我的观察我永远不会被取消。服务结构RunAsync(CancellationToken cancellationToken)不会被取消
当然取消RunAsync方法和调用OnCloseAsync将是一种多余。 我还想知道何时(如果)取消实际发生。
我应该写一些额外的代码来提供一个工作的Stop()方法在我的客户端?我会在者优先中的CancellationToken将RunAsync实际上被取消;-)
我的服务织物服务代码:
/// <summary>
/// This is the main entry point for your service instance.
/// </summary>
/// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param>
protected override async Task RunAsync(CancellationToken cancellationToken)
{
// TODO: Replace the following sample code with your own logic
// or remove this RunAsync override if it's not needed in your service.
long iterations = 0;
while (!cancellationToken.IsCancellationRequested)
{
// I commented this out because I want my client to handle the cancellation
// event gracefully without throwing an OperationCanceled exception.
//cancellationToken.ThrowIfCancellationRequested();
// I never found these messages in any logs. Nor in the diagnostics events window in Visual Studio.
ServiceEventSource.Current.ServiceMessage(this, "Working-{0}", ++iterations);
await _client.Start(cancellationToken);
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
}
}
我的样品客户实现:
public class Client
{
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
public async Task Start(CancellationToken cancellationToken = default(CancellationToken))
{
while (!cancellationToken.IsCancellationRequested)
{
_logger.Info("Saying hello from Main Operation.");
await Task.Delay(3000, cancellationToken);
}
_logger.Info("Cancellation requested. Shutting down MainOperation().");
}
public void Stop()
{
_logger.Info("Stop requested. But I have no means to stop. Not implemented.");
}
}
再次,执行此操作的一种方法是使用DeleteServiceAsync以编程方式删除服务,使用DeleteServiceAsync也可以使用CreateServiceAsync重新创建它。 – LoekD
当然。但我不想在Service Fabric之外运行任何代码。 OnCloseAsync也被调用,这很好。我可以与此合作。我仍然想知道RunAsync的cancellationToken是否被设置为“取消”。 – lapsus
你可以从其他服务中调用它。当服务停止,升级,从辅助副本迁移到主副本和/或在群集中重新定位时调用。 – LoekD