コード例 #1
0
        public override Task RunAsync(CancellationToken cancellationToken)
        {
            return(Task.Run(async() =>
            {
                try
                {
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        IQueueMessage message = null;
                        bool executionSucceeded = false;
                        try
                        {
                            do
                            {
                                message = await _queueReader.GetMessageAsync();
                                if (message == null)
                                {
                                    break;
                                }

                                var context = new QueueTriggeringContext(message.InsertionTime);

                                var p = new List <object>()
                                {
                                    message.Value(_parameterType)
                                };

                                if (_hasSecondParameter)
                                {
                                    p.Add(_useTriggeringContext ? context : (object)message.InsertionTime);
                                }

                                await Invoke(_serviceProvider, _method, p.ToArray());
                                await ProcessCompletedMessage(message, context);
                                executionSucceeded = true;
                            } while (!cancellationToken.IsCancellationRequested);
                        }
                        catch (Exception ex)
                        {
                            await LogError("QueueTriggerBinding", "RunAsync", ex);
                            await ProcessFailedMessage(message);
                            executionSucceeded = false;
                        }
                        finally
                        {
                            await Task.Delay(_delayStrategy.GetNextDelay(executionSucceeded), cancellationToken);
                        }
                    }
                }
                finally
                {
                    await _log.WriteInfoAsync("QueueTriggerBinding", "RunAsync", _queueName, "Process ended");
                }
            }, cancellationToken));
        }
コード例 #2
0
        public override Task RunAsync(CancellationToken cancellationToken)
        {
            return(Task.Factory.StartNew(async() =>
            {
                Exception globalEx = null;
                try
                {
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        IQueueMessage message = null;
                        bool executionSucceeded = false;
                        try
                        {
                            do
                            {
                                message = await _queueReader.GetMessageAsync();
                                if (message == null)
                                {
                                    break;
                                }

                                var context = new QueueTriggeringContext(message.InsertionTime);

                                var p = new List <object>()
                                {
                                    message.Value(_parameterType)
                                };

                                if (_hasSecondParameter)
                                {
                                    p.Add(_useTriggeringContext ? context : (object)message.InsertionTime);
                                }

                                await Invoke(_serviceProvider, _method, p.ToArray());
                                await ProcessCompletedMessage(message, context);
                                executionSucceeded = true;
                            } while (!cancellationToken.IsCancellationRequested);
                        }
                        catch (Exception ex)
                        {
                            await LogError("QueueTriggerBinding", "RunAsync", ex);
                            await ProcessFailedMessage(message);
                            executionSucceeded = false;
                        }
                        finally
                        {
                            await Task.Delay(_delayStrategy.GetNextDelay(executionSucceeded), cancellationToken);
                        }
                    }
                }
                catch (Exception ex)
                {
                    globalEx = ex;
                }
                finally
                {
                    var msg =
                        $"Process ended. Exception={globalEx?.Message + globalEx?.StackTrace}. Token.IsCancellationRequested={cancellationToken.IsCancellationRequested}";
                    await _log.WriteInfoAsync("QueueTriggerBinding", "RunAsync", _queueName, msg);
                }
            }, cancellationToken, TaskCreationOptions.LongRunning | TaskCreationOptions.DenyChildAttach, TaskScheduler.Default).Unwrap());
        }