Exemplo n.º 1
0
        async Task IBrokeredMessageReceiver.Handle(Message message, Action <ReceiveContext> contextCallback)
        {
            var context = new ServiceBusReceiveContext(_inputAddress, message, _receiveEndpointContext);

            contextCallback?.Invoke(context);

            var messageReceiver = context.GetPayload <Microsoft.Azure.ServiceBus.Core.IMessageReceiver>();

            if (messageReceiver == null)
            {
                throw new Exception($"Message receiver: {nameof(Microsoft.Azure.ServiceBus.Core.IMessageReceiver)} is missing in context Payload");
            }

            try
            {
                await _receiveEndpointContext.ReceiveObservers.PreReceive(context).ConfigureAwait(false);

                if (message.SystemProperties.LockedUntilUtc <= DateTime.UtcNow)
                {
                    throw new MessageLockExpiredException(_inputAddress, $"The message lock expired: {message.MessageId}");
                }

                if (message.ExpiresAtUtc < DateTime.UtcNow)
                {
                    throw new MessageTimeToLiveExpiredException(_inputAddress, $"The message TTL expired: {message.MessageId}");
                }

                await _receiveEndpointContext.ReceivePipe.Send(context).ConfigureAwait(false);

                await context.ReceiveCompleted.ConfigureAwait(false);

                await messageReceiver.CompleteAsync(message.SystemProperties.LockToken).ConfigureAwait(false);

                await _receiveEndpointContext.ReceiveObservers.PostReceive(context).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                try
                {
                    await messageReceiver.AbandonAsync(message.SystemProperties.LockToken).ConfigureAwait(false);
                }
                catch (Exception exception)
                {
                    if (_log.IsWarnEnabled)
                    {
                        _log.Warn($"Abandon message faulted: {message.MessageId}", exception);
                    }
                }

                await _receiveEndpointContext.ReceiveObservers.ReceiveFault(context, ex).ConfigureAwait(false);
            }
            finally
            {
                context.Dispose();
            }
        }