c# – Service Fabric RunAsync(CancellationToken cancellationToken)不会被取消
作者:互联网
我认为RunAsync方法将CancellationToken作为一个很好的参数.不幸的是,我的观察结果从未被取消.
当然,取消RunAsync方法并调用OnCloseAsync会有点多余.我仍然想知道取消实际发生的时间(如果).
我应该写一些额外的代码来在我的客户端提供一个工作的Stop()方法吗?我本来希望RunAsync中的cancellationToken实际上会被取消;-)
我的服务架构服务代码:
/// <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.");
}
}
解决方法:
是的,取消令牌实际上已取消.这是有保证的.我可以向你保证,经过多年的测试和生产使用,这不是一个疏忽.
但是,您的代码存在疏忽.
如果您希望从客户端看到此跟踪输出:
_logger.Info("Cancellation requested. Shutting down MainOperation().");
你不会,更不可能你会看到它.为什么?因为之前这行:
await Task.Delay(3000, cancellationToken);
在延迟期间发出取消令牌信号时,将抛出OperationCanceledException.这将使您退出循环并退出RunAsync,因此您的日志记录行将不会执行.
由于您在该延迟中花费3秒,并且在循环中花费超过它的纳秒,您可以看到为什么当您不在延迟内时取消会发生的可能性极小.
标签:c,azure-service-fabric 来源: https://codeday.me/bug/20190608/1199082.html