.net core 连续作业队列在关闭前完成作业

.net core 连续作业队列在关闭前完成作业,.net-core,background-service,tpl-dataflow,.net Core,Background Service,Tpl Dataflow,使用下面的代码,我正在使用一个带有连续作业队列的.net core后台服务,其中ExecuteAsync模拟已添加到队列中的作业(这可能是收集订单、生成订单响应等) 后台服务: public class Worker : BackgroundService { public WorkerJobQueue orderQueue { get; set; } public override async Task StartAsync(CancellationToken cancel

使用下面的代码,我正在使用一个带有连续作业队列的.net core后台服务,其中ExecuteAsync模拟已添加到队列中的作业(这可能是收集订单、生成订单响应等)

后台服务:

public class Worker : BackgroundService
{

    public WorkerJobQueue orderQueue { get; set; }

    public override async Task StartAsync(CancellationToken cancellationToken)
    {
        Console.WriteLine("Sales_Order_Processor_Service Starting");
        
        orderQueue = new WorkerJobQueue();
        orderQueue.RegisterHandler<TestJob>(TestJobWorker.DoJob);

        await base.StartAsync(cancellationToken);
    }

    public override async Task StopAsync(CancellationToken cancellationToken)
    {
        Console.WriteLine("Sales_Order_Processor_Service Stopping");
        await orderQueue.EndQueue(cancellationToken);
        await base.StopAsync(cancellationToken);
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        var i = 0;

        while (!stoppingToken.IsCancellationRequested)
        {
            Console.ReadLine();

            for (var j = 0; j < 50; j++)
            {
                var tmp = new TestJob { JobNumber = i };
                Console.WriteLine($"Adding job {tmp.JobNumber} to queue");
                await orderQueue.Enqueue(tmp);
                i++;
            }

            Console.WriteLine($"{orderQueue.GetNumberOfRemainingJobs()} Jobs in queue...");

        }
    }

}
为了模拟作业所做的一些工作:

    public class TestJobWorker
{
   
    public static void DoJob(TestJob testJob)
    {
        var rnd = new Random();
        var ranNum = rnd.Next(10);

        Console.WriteLine($"Starting job {testJob.JobNumber} sleeping for {ranNum} seconds");
        System.Threading.Thread.Sleep(ranNum* 1000);
        Console.WriteLine($"Finished job {testJob.JobNumber}");
    }

}
作业队列的工作原理是,它应该在按键时向队列中添加50个作业,但是,每当服务停止/控制台窗口关闭时,它实际上并没有等待作业队列完成,也没有真正进入
stopsync
功能

我是否理解在关闭/停止服务时调用函数
StopAsync

或者我的队列中的逻辑在结束队列时实际上不正确?

结果是
StopAsync
的调用晚于托管的
stop
我使用以下代码在stop时触发:

        private readonly IHostApplicationLifetime _hostApplicationLifetime;
        public Worker(IHostApplicationLifetime hostApplicationLifetime)
        {
            _hostApplicationLifetime = hostApplicationLifetime;
        }

        private void OnStopping()
        {
            orderQueue.EndQueue();
            logger.Debug("Sales_Order_Processor_Service Stopping");
        }
    public class TestJobWorker
{
   
    public static void DoJob(TestJob testJob)
    {
        var rnd = new Random();
        var ranNum = rnd.Next(10);

        Console.WriteLine($"Starting job {testJob.JobNumber} sleeping for {ranNum} seconds");
        System.Threading.Thread.Sleep(ranNum* 1000);
        Console.WriteLine($"Finished job {testJob.JobNumber}");
    }

}
        private readonly IHostApplicationLifetime _hostApplicationLifetime;
        public Worker(IHostApplicationLifetime hostApplicationLifetime)
        {
            _hostApplicationLifetime = hostApplicationLifetime;
        }

        private void OnStopping()
        {
            orderQueue.EndQueue();
            logger.Debug("Sales_Order_Processor_Service Stopping");
        }