StopAsync和BackgroundService上的ExecuteAsync

问题描述 投票:3回答: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
    } }
c# asp.net-core-2.1
1个回答
0
投票

这是一个很老的线程,但是我看不到您实现IHostService或BackgroundService。

© www.soinside.com 2019 - 2024. All rights reserved.