c# - 在 BackgroundService 上使用 ExecuteAsync 的 StopAsync
问题描述
我正在使用托管服务进行一些长时间运行的操作(检查打开的连接)。
在使用 IHostedService 和 BackgroundService 类在微服务中实现后台任务的示例中,有两行在应用程序关闭时记录信息(在列出的代码中双引号)
但在我的场景中,我永远无法获得第二个日志信息?我想知道如果无法访问,为什么微软会放置第二行日志记录。谁能给我解释一下?
public class GracePeriodManagerService : BackgroundService {
private readonly ILogger<GracePeriodManagerService> _logger;
private readonly OrderingBackgroundSettings _settings;
private readonly IEventBus _eventBus;
public GracePeriodManagerService(IOptions<OrderingBackgroundSettings>> settings,
IEventBus eventBus,
ILogger<GracePeriodManagerService> logger)
{
//Constructor’s parameters validations...
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogDebug($"GracePeriodManagerService is starting.");
stoppingToken.Register(() =>
_logger.LogDebug($" GracePeriod background task is stopping."));
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogDebug($"GracePeriod task doing background work.");
// This eShopOnContainers method is querying a database table
// and publishing events into the Event Bus (RabbitMS / ServiceBus)
CheckConfirmedGracePeriodOrders();
await Task.Delay(_settings.CheckUpdateTime, stoppingToken);
}
_logger.LogDebug($"GracePeriod background task is stopping.");
}
protected override async Task StopAsync (CancellationToken stoppingToken)
{
// Run your graceful clean-up actions
}
}
解决方案
推荐阅读
- python - 如何修复索引值 1 超出范围。无法通过 twitter 获取状态函数正确传递变量
- vuejs2 - Vue js - 在 DOM 中声明和使用变量
- python - 如何通过 ssh 将加密密码从一个脚本传递到另一个脚本?
- vbscript - 如何检查单元格中是否存在数组值?
- java - 正则表达式:在字符串中查找模式
- python - 如何修复处理 CIFAR-10 数据集、构建 CNN 的维度(值)错误?
- excel - 这个 excel 文件中的列表值是从哪里获取的?
- javascript - 如何对数组中的每个对象执行计算,然后输出该数组?
- leveldb - python和java之间的levelDB性能差异
- java - 为什么按钮的意图不起作用