示例#1
0
        /// <summary>
        /// Обработать полученное сообщение
        /// </summary>
        private async Task ProcessReceivedMessageAsync(object model, BasicDeliverEventArgs eventArgs)
        {
            var messageJson = Encoding.UTF8.GetString(eventArgs.Body.ToArray());

            _logger.Information($"Received interservice message");
            _logger.Debug($"Interservice message = '{messageJson}'");

            var messageJObject  = messageJson.ToJObject();
            var messageTypeName = messageJObject.Value <string>(nameof(IInterserviceMessageModel.MessageType));

            if (!Enum.TryParse <InterserviceMessageTypeEnum>(messageTypeName, out var messageType))
            {
                throw new InterserviceMqException($"Unknown interservice message type: '{messageTypeName}'");
            }
            if (!_messageConsumerDictionary.TryGetValue(messageType, out var messageConsumer))
            {
                throw new InterserviceMqException($"Interservice message consumer for type '{messageType}' is not implemented");
            }

            _logger.Information($"Consuming interservice message with routing key '{eventArgs.RoutingKey}'...");

            await messageConsumer.ConsumeAsync(messageJson);

            _logger.Information($"Interservice message with routing key '{eventArgs.RoutingKey}' was successfully consumed");
        }
示例#2
0
        public async Task <HttpResponseWithBodyModel <T> > SendRequestAsync <T>(HttpRequestModel requestModel)
        {
            using var httpResponse = await SendRequestCoreAsync(requestModel);

            if (!httpResponse.IsSuccessStatusCode)
            {
                return(new HttpResponseWithBodyModel <T>(httpResponse));
            }

            _logger.Debug("Reading HTTP response body...");

            using var httpResponseBodyStream = await httpResponse.Content.ReadAsStreamAsync();

            var httpResponseBody = httpResponseBodyStream.FromJson <T>();

            _logger.Debug($"HTTP response body was successfully read ({httpResponseBody.ToJson()})");

            return(new HttpResponseWithBodyModel <T>(httpResponse)
            {
                Body = httpResponseBody
            });
        }
示例#3
0
        public void PublishMessage(InterserviceQueueTypeEnum queueType, IInterserviceMessageModel messageModel)
        {
            _logger.Information($"Publishing interservice message to queue with type = '{queueType}'...");
            _logger.Debug($"Interservice message = '{messageModel.ToJson()}'");

            var connection = _rabbitConnectionService.GetConnection();

            using var channelModel = connection.CreateModel();

            var queueDeclareResult = channelModel.QueueDeclare(queueType);

            var messageJson  = messageModel.ToJson();
            var messageBytes = Encoding.UTF8.GetBytes(messageJson);

            channelModel.BasicPublish("", queueDeclareResult.QueueName, null, messageBytes);

            _logger.Information($"Interservice message was successfully published to queue with type = '{queueType}'");
        }