Esempio n. 1
0
        internal async Task EnqueueEvent(QueuedEventWrapperBase @event, CancellationToken cancellationToken)
        {
            await _semaphoreSlim.WaitAsync(cancellationToken);

            _eventQueue.Enqueue(@event);

            _semaphoreSlim.Release();
        }
        /// <summary>
        /// Hosted service execute method
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            await Task.Run(async() =>
            {
                while (!stoppingToken.IsCancellationRequested)
                {
                    if (!await _backgroundEventQueue.HasEvents(stoppingToken))
                    {
                        continue;
                    }

                    QueuedEventWrapperBase job = await _backgroundEventQueue.DequeueEvent(stoppingToken);

                    try
                    {
                        await job.Handle(stoppingToken);
                    }
                    catch (AggregateException ex)
                    {
                        await _exceptionHandler.Handle(ex, job.EventName);
                    }
                }
            }, stoppingToken);
        }