C# StopAsync与BackgroundService上的ExecuteAsync
我正在使用托管服务执行一些长时间运行的操作(检查打开的连接) 在上的示例中,有两行记录应用程序关闭时的信息(在列出的代码中双引号) 但在我的场景中,我永远无法访问第二个日志信息? 我想知道,如果无法访问,为什么微软会将日志放在第二行。谁能给我解释一下吗C# StopAsync与BackgroundService上的ExecuteAsync,c#,asp.net-core-2.1,C#,Asp.net Core 2.1,我正在使用托管服务执行一些长时间运行的操作(检查打开的连接) 在上的示例中,有两行记录应用程序关闭时的信息(在列出的代码中双引号) 但在我的场景中,我永远无法访问第二个日志信息? 我想知道,如果无法访问,为什么微软会将日志放在第二行。谁能给我解释一下吗 public class GracePeriodManagerService : BackgroundService { private readonly ILogger<GracePeriodManagerServ
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
}
}
公共类GracePeriodManagerService:BackgroundService{
专用只读ILogger\u记录器;
私人只读订单背景设置\u设置;
私有只读IEventBus\u事件总线;
公共GracePeriodManagerService(IOOptions>settings,
IEventBus事件总线,
ILogger(记录器)
{
//构造函数的参数验证。。。
}
受保护的覆盖异步任务ExecuteAsync(CancellationToken stoppingToken)
{
_LogDebug($“GracePeriodManagerService正在启动。”);
stoppingToken.Register(()=>
_LogDebug($“GracePeriod后台任务正在停止。”);
同时(!stoppingToken.IsCancellationRequested)
{
_LogDebug($“GracePeriod任务做后台工作”);
//此eShopOnContainers方法正在查询数据库表
//并将事件发布到事件总线(RabbitMS/ServiceBus)
CheckConfirmedGracePeriodOrders();
等待任务。延迟(_settings.CheckUpdateTime,stoppingToken);
}
_LogDebug($“GracePeriod后台任务正在停止。”);
}
受保护的覆盖异步任务StopAsync(CancellationToken stoppingToken)
{
//进行优雅的清理动作
}
}