Esempio n. 1
0
        public async Task SendMessageAsync(TMessage message)
        {
            try
            {
                #region Sending Message
                CheckConnection();

                await queueClient.SendAsync(new BrokeredMessage(new MemoryStream(MessageQueueCommonItems.SerializeToJsonBytes(message))));

                #endregion
            }
            catch (QueueException queueException)
            {
                #region Logging - Error
                logger.Fatal(queueException, queueException.Message);
                #endregion

                throw;
            }
            catch (Exception ex)
            {
                throw MessageQueueCommonItems.PrepareAndLogQueueException(
                          errorCode: QueueErrorCode.FailedToSendMessage,
                          message: ErrorMessages.FailedToSendMessage,
                          innerException: ex,
                          queueContext: CommonItems.ServiceBusName,
                          queueName: sbConfiguration.QueueName,
                          address: sbConfiguration.Address,
                          logger: logger);
            }
        }
Esempio n. 2
0
        public void SendRequest(TRequest message)
        {
            #region Sending Message
            try
            {
                // Preparing message.
                var correlationId     = Guid.NewGuid().ToString("N");
                var messageProperties = model.CreateBasicProperties();
                messageProperties.ReplyTo       = replyQueueName;
                messageProperties.CorrelationId = correlationId;

                // Storing correlation Id.
                lock (correlationIds)
                {
                    correlationIds.Add(correlationId);
                }

                // Sending message.
                lock (lockForQueueOperation)
                {
                    model.BasicPublish(exchangeName, routingKey, messageProperties, MessageQueueCommonItems.SerializeToJsonBytes(message));
                }
            }
            catch (QueueException queueException)
            {
                #region Logging - Error
                logger.Fatal(queueException, queueException.Message);
                #endregion

                throw;
            }
            catch (Exception ex)
            {
                #region Adding Context Data
                var context = new Dictionary <string, string>();

                if (!string.IsNullOrEmpty(rabbitMqConfiguration.ExchangeName))
                {
                    context.Add(CommonContextKeys.ExchangeName, rabbitMqConfiguration.ExchangeName);
                }

                if (!string.IsNullOrEmpty(routingKey))
                {
                    context.Add(CommonContextKeys.RoutingKey, routingKey);
                }
                #endregion

                throw MessageQueueCommonItems.PrepareAndLogQueueException(
                          errorCode: QueueErrorCode.FailedToSendMessage,
                          message: ErrorMessages.FailedToSendMessage,
                          innerException: ex,
                          queueContext: CommonItems.RabbitMqName,
                          queueName: rabbitMqConfiguration.QueueName,
                          address: rabbitMqConfiguration.Address,
                          context: context,
                          logger: logger);
            }
            #endregion
        }
Esempio n. 3
0
        public void SendMessage(TMessage message)
        {
            try
            {
                #region Sending Message
                lock (lockForQueueOperation)
                {
                    model.BasicPublish(exchangeName, routingKey, modelProperties, MessageQueueCommonItems.SerializeToJsonBytes(message));
                }
                #endregion
            }
            catch (QueueException queueException)
            {
                #region Logging - Error
                logger.Fatal(queueException, queueException.Message);
                #endregion

                throw;
            }
            catch (Exception ex)
            {
                #region Adding Context Data
                var context = new Dictionary <string, string>();

                if (!string.IsNullOrEmpty(rabbitMqConfiguration.ExchangeName))
                {
                    context.Add(CommonContextKeys.ExchangeName, rabbitMqConfiguration.ExchangeName);
                }

                if (!string.IsNullOrEmpty(routingKey))
                {
                    context.Add(CommonContextKeys.RoutingKey, routingKey);
                }
                #endregion

                throw MessageQueueCommonItems.PrepareAndLogQueueException(
                          errorCode: QueueErrorCode.FailedToSendMessage,
                          message: ErrorMessages.FailedToSendMessage,
                          innerException: ex,
                          queueContext: CommonItems.RabbitMqName,
                          queueName: rabbitMqConfiguration.QueueName,
                          address: rabbitMqConfiguration.Address,
                          context: context,
                          logger: logger);
            }
        }
        public override void Response(TResponse response)
        {
            try
            {
                #region Sending Response
                // Preparing response.
                var responseBytes = MessageQueueCommonItems.SerializeToJsonBytes(response);

                // Sending response.
                model.BasicPublish(exchange, routingKey, replyProperties, responseBytes);

                // Acknowledging message.
                if (acknowledgment)
                {
                    model.BasicAck(deliveryTag, false);
                }
                #endregion
            }
            catch (QueueException queueException)
            {
                #region Logging - Error
                logger.Error(queueException, queueException.Message);
                #endregion

                throw;
            }
            catch (Exception ex)
            {
                throw MessageQueueCommonItems.PrepareAndLogQueueException(
                          errorCode: QueueErrorCode.FailedToSendResponseMessage,
                          message: ErrorMessages.FailedToSendResponseMessage,
                          innerException: ex,
                          queueContext: CommonItems.RabbitMqName,
                          context: new Dictionary <string, string>
                {
                    [CommonContextKeys.ExchangeName] = exchange,
                    [CommonContextKeys.RoutingKey]   = routingKey,
                    [ContextKeys.ReplyTo]            = replyProperties.ReplyTo
                },
                          logger: logger);
            }
        }