示例#1
0
        /// <summary>
        /// Отправить HTTP-запрос (core-метод)
        /// </summary>
        private async Task <HttpResponseMessage> SendRequestCoreAsync(HttpRequestModel requestModel)
        {
            var httpRequestLogText = $"HTTP {requestModel.Method.Method} request to uri '{requestModel.Uri}'";

            _logger.Information($"Sending {httpRequestLogText}...");

            using var httpRequest = new HttpRequestMessage(requestModel.Method, requestModel.Uri);

            foreach (var httpHeader in requestModel.Headers)
            {
                httpRequest.Headers.Add(httpHeader.Key, httpHeader.Value);
            }

            if (requestModel.Body != null)
            {
                var httpRequestBodyJson = requestModel.Body.ToJson();
                httpRequest.Content = new StringContent(httpRequestBodyJson, Encoding.UTF8, MediaTypeNames.Application.Json);

                _logger.Trace($"HTTP request body = '{httpRequestBodyJson}'");
            }

            var httpResponse = await _httpClient.SendAsync(httpRequest);

            if (httpResponse.IsSuccessStatusCode)
            {
                _logger.Information($"{httpRequestLogText} was successfully sent");
            }
            else
            {
                _logger.Warning($"{httpRequestLogText} failed (status code = '{httpResponse.StatusCode}')");
            }

            return(httpResponse);
        }
示例#2
0
        public IConnection GetConnection(bool createIfNotExists = true)
        {
            _logger.Information("Getting connection to RabbitMQ server...");

            if (_connection == null || !_connection.IsOpen)
            {
                if (createIfNotExists)
                {
                    _logger.Warning(NO_CONNECTION_MESSAGE);

                    CreateConnection();
                }
                else
                {
                    throw new RabbitException(NO_CONNECTION_MESSAGE);
                }
            }

            return(_connection);
        }