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

            contextCallback?.Invoke(context);

            context.TryGetPayload <Microsoft.Azure.ServiceBus.Core.IReceiverClient>(out var messageReceiver);

            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);

                if (messageReceiver != null)
                {
                    await messageReceiver.CompleteAsync(message.SystemProperties.LockToken).ConfigureAwait(false);
                }

                await _receiveEndpointContext.ReceiveObservers.PostReceive(context).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                await _receiveEndpointContext.ReceiveObservers.ReceiveFault(context, ex).ConfigureAwait(false);

                if (messageReceiver == null)
                {
                    throw;
                }

                try
                {
                    await messageReceiver.AbandonAsync(message.SystemProperties.LockToken).ConfigureAwait(false);
                }
                catch (Exception exception)
                {
                    if (_log.IsWarnEnabled)
                    {
                        _log.Warn($"Abandon message faulted: {message.MessageId}", exception);
                    }
                }
            }
            finally
            {
                context.Dispose();
            }
        }
Beispiel #2
0
        async Task IMessageSessionAsyncHandler.OnMessageAsync(MessageSession session, BrokeredMessage message)
        {
            if (_receiver.IsShuttingDown)
            {
                await WaitAndAbandonMessage(message).ConfigureAwait(false);

                return;
            }

            using (var delivery = _tracker.BeginDelivery())
            {
                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Receiving {0}:{1}({3}) - {2}", delivery.Id, message.MessageId, _receiver.QueuePath, session.SessionId);
                }

                var context = new ServiceBusReceiveContext(_receiver.InputAddress, message, _context, _sendEndpointProvider, _publishEndpointProvider);

                context.GetOrAddPayload <MessageSessionContext>(() => new BrokeredMessageSessionContext(session));
                context.GetOrAddPayload(() => _context);

                try
                {
                    await _context.PreReceive(context).ConfigureAwait(false);

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

                    await context.CompleteTask.ConfigureAwait(false);

                    await message.CompleteAsync().ConfigureAwait(false);

                    await _context.PostReceive(context).ConfigureAwait(false);

                    if (_log.IsDebugEnabled)
                    {
                        _log.DebugFormat("Receive completed: {0}", message.MessageId);
                    }
                }
                catch (Exception ex)
                {
                    if (_log.IsErrorEnabled)
                    {
                        _log.Error($"Received faulted: {message.MessageId}", ex);
                    }

                    await message.AbandonAsync().ConfigureAwait(false);

                    await _context.ReceiveFault(context, ex).ConfigureAwait(false);
                }
                finally
                {
                    context.Dispose();
                }
            }
        }
        async Task IBrokeredMessageReceiver.Handle(BrokeredMessage message, Action <ReceiveContext> contextCallback)
        {
            var context = new ServiceBusReceiveContext(_inputAddress, message, _receiveObservers, _receiveTopology);

            contextCallback?.Invoke(context);

            try
            {
                await _receiveObservers.PreReceive(context).ConfigureAwait(false);

                if (message.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 _receivePipe.Send(context).ConfigureAwait(false);

                await context.CompleteTask.ConfigureAwait(false);

                await message.CompleteAsync().ConfigureAwait(false);

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

                await _receiveObservers.ReceiveFault(context, ex).ConfigureAwait(false);
            }
            finally
            {
                context.Dispose();
            }
        }
Beispiel #4
0
        async Task OnMessage(BrokeredMessage message)
        {
            if (_shuttingDown)
            {
                await WaitAndAbandonMessage(message).ConfigureAwait(false);

                return;
            }

            using (var delivery = _tracker.BeginDelivery())
            {
                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Receiving {0}:{1} - {2}", delivery.Id, message.MessageId, _clientSettings.Path);
                }

                var context = new ServiceBusReceiveContext(_clientContext.InputAddress, message, _context, _topology);
                context.GetOrAddPayload(() => _context);

                try
                {
                    await _context.PreReceive(context).ConfigureAwait(false);

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

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

                    await _receivePipe.Send(context).ConfigureAwait(false);

                    await context.CompleteTask.ConfigureAwait(false);

                    await message.CompleteAsync().ConfigureAwait(false);

                    await _context.PostReceive(context).ConfigureAwait(false);

                    if (_log.IsDebugEnabled)
                    {
                        _log.DebugFormat("Receive completed: {0}", message.MessageId);
                    }
                }
                catch (Exception ex)
                {
                    if (_log.IsErrorEnabled)
                    {
                        _log.Error($"Received faulted: {message.MessageId}", ex);
                    }

                    await AbandonMessage(message).ConfigureAwait(false);

                    await _context.ReceiveFault(context, ex).ConfigureAwait(false);
                }
                finally
                {
                    context.Dispose();
                }
            }
        }
        async Task IBrokeredMessageReceiver.Handle(Message message, Action <ReceiveContext> contextCallback)
        {
            LogContext.Current = _context.LogContext;

            var context = new ServiceBusReceiveContext(_inputAddress, message, _context);

            contextCallback?.Invoke(context);

            context.TryGetPayload <MessageLockContext>(out var lockContext);

            var activity = LogContext.IfEnabled(OperationName.Transport.Receive)?.StartActivity();

            activity.AddReceiveContextHeaders(context);

            try
            {
                if (_context.ReceiveObservers.Count > 0)
                {
                    await _context.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 _context.ReceivePipe.Send(context).ConfigureAwait(false);

                await context.ReceiveCompleted.ConfigureAwait(false);

                if (lockContext != null)
                {
                    await lockContext.Complete().ConfigureAwait(false);
                }

                if (_context.ReceiveObservers.Count > 0)
                {
                    await _context.ReceiveObservers.PostReceive(context).ConfigureAwait(false);
                }
            }
            catch (SessionLockLostException ex)
            {
                LogContext.Warning?.Log(ex, "Session Lock Lost: {MessageId", message.MessageId);

                if (_context.ReceiveObservers.Count > 0)
                {
                    await _context.ReceiveObservers.ReceiveFault(context, ex).ConfigureAwait(false);
                }
            }
            catch (MessageLockLostException ex)
            {
                LogContext.Warning?.Log(ex, "Session Lock Lost: {MessageId", message.MessageId);

                if (_context.ReceiveObservers.Count > 0)
                {
                    await _context.ReceiveObservers.ReceiveFault(context, ex).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                if (_context.ReceiveObservers.Count > 0)
                {
                    await _context.ReceiveObservers.ReceiveFault(context, ex).ConfigureAwait(false);
                }

                if (lockContext == null)
                {
                    throw;
                }

                try
                {
                    await lockContext.Abandon(ex).ConfigureAwait(false);
                }
                catch (Exception exception)
                {
                    LogContext.Warning?.Log(exception, "Abandon message faulted: {MessageId", message.MessageId);
                }
            }
            finally
            {
                activity?.Stop();

                context.Dispose();
            }
        }
        async Task IMessageSessionAsyncHandler.OnMessageAsync(MessageSession session, BrokeredMessage message)
        {
            if (_receiver.IsShuttingDown)
            {
                await WaitAndAbandonMessage(message).ConfigureAwait(false);
                return;
            }

            using (var delivery = _tracker.BeginDelivery())
            {
                if (_log.IsDebugEnabled)
                    _log.DebugFormat("Receiving {0}:{1}({3}) - {2}", delivery.Id, message.MessageId, _receiver.QueuePath, session.SessionId);

                var context = new ServiceBusReceiveContext(_receiver.InputAddress, message, _context, _sendEndpointProvider, _publishEndpointProvider);

                context.GetOrAddPayload<MessageSessionContext>(() => new BrokeredMessageSessionContext(session));
                context.GetOrAddPayload(() => _context);

                try
                {
                    await _context.PreReceive(context).ConfigureAwait(false);

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

                    await context.CompleteTask.ConfigureAwait(false);

                    await message.CompleteAsync().ConfigureAwait(false);

                    await _context.PostReceive(context).ConfigureAwait(false);

                    if (_log.IsDebugEnabled)
                        _log.DebugFormat("Receive completed: {0}", message.MessageId);
                }
                catch (Exception ex)
                {
                    if (_log.IsErrorEnabled)
                        _log.Error($"Received faulted: {message.MessageId}", ex);

                    await message.AbandonAsync().ConfigureAwait(false);
                    await _context.ReceiveFault(context, ex).ConfigureAwait(false);
                }
                finally
                {
                    context.Dispose();
                }
            }
        }