StopAsync与BackgroundService上的ExecuteAsync

时间:2018-10-22 12:54:09

标签: c# asp.net-core-2.1

我正在使用托管服务执行一些长时间运行的操作(检查打开的连接)。

Implement background tasks in microservices with IHostedService and the BackgroundService class的示例中,有两行记录了应用程序关闭时的信息(在列出的代码中双引号)

但是在我的情况下,我永远无法获得第二次日志信息? 我想知道为什么Microsoft如果无法访问则将第二行日志记录下来。有人可以向我解释吗?

  

公共类GracePeriodManagerService:BackgroundService {
      私有只读ILogger _logger;       私人只读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
    } }

0 个答案:

没有答案