コード例 #1
0
        public void Receive(Func <IReceiveContext, Action <IReceiveContext> > callback, TimeSpan timeout)
        {
            AddManagementBinding();

            AddReceiverBinding();

            _connectionHandler.Use(connection =>
            {
                BrokeredMessage message = _receiver.Get(timeout);

                if (message == null)
                {
                    return;
                }

                using (var body = new MemoryStream(message.GetBody <MessageEnvelope>().ActualBody, false))
                {
                    ReceiveContext context = ReceiveContext.FromBodyStream(body);
                    context.SetMessageId(message.MessageId);
                    context.SetInputAddress(Address);
                    context.SetCorrelationId(message.CorrelationId);
                    context.SetContentType(message.ContentType);

                    if (_logger.IsDebugEnabled)
                    {
                        TraceMessage(context);
                    }

                    Action <IReceiveContext> receive = callback(context);
                    if (receive == null)
                    {
                        Address.LogSkipped(message.MessageId);
                        return;
                    }

                    receive(context);

                    try
                    {
                        message.Complete();
                    }
                    catch (MessageLockLostException ex)
                    {
                        if (_logger.IsErrorEnabled)
                        {
                            _logger.Error("Message Lock Lost on message Complete()", ex);
                        }
                    }
                    catch (MessagingException ex)
                    {
                        if (_logger.IsErrorEnabled)
                        {
                            _logger.Error("Generic MessagingException thrown", ex);
                        }
                    }
                }
            });
        }
        public void Receive(Func <IReceiveContext, Action <IReceiveContext> > callback, TimeSpan timeout)
        {
            AddConsumerBinding();

            _connectionHandler.Use(connection =>
            {
                BrokeredMessage message = _consumer.Get(timeout);
                if (message == null)
                {
                    return;
                }

                using (var stream = message.GetBody <Stream>())
                {
                    ReceiveContext context = ReceiveContext.FromBodyStream(stream);
                    context.SetMessageId(message.MessageId);
                    context.SetInputAddress(Address);
                    context.SetCorrelationId(message.CorrelationId);
                    context.SetContentType(message.ContentType);

                    Action <IReceiveContext> receive = callback(context);
                    if (receive == null)
                    {
                        Address.LogSkipped(message.MessageId);
                        return;
                    }

                    try
                    {
                        receive(context);
                    }
                    catch (Exception ex)
                    {
                        if (_logger.IsErrorEnabled)
                        {
                            _logger.Error("Consumer threw an exception", ex);
                        }

                        message.Abandon();
                    }

                    try
                    {
                        message.Complete();
                    }
                    catch (MessageLockLostException ex)
                    {
                        if (_logger.IsErrorEnabled)
                        {
                            _logger.Error("Message Lock Lost on message Complete()", ex);
                        }
                    }
                    catch (MessagingException ex)
                    {
                        if (_logger.IsErrorEnabled)
                        {
                            _logger.Error("Generic MessagingException thrown", ex);
                        }
                    }
                }
            });
        }