Example #1
0
        private static async Task <XmlReader> GetXmlContent(HttpResponseMessage response, string trackingId)
        {
            try
            {
                if (response.Content == null)
                {
                    return(XmlReader.Create(new StringReader(string.Empty)));
                }

                var xmlReader = XmlReader.Create(await response.Content.ReadAsStreamAsync());
                if (xmlReader.ReadToFollowing("entry"))
                {
                    if (xmlReader.ReadToDescendant("content"))
                    {
                        xmlReader.ReadStartElement();
                    }
                }

                return(xmlReader);
            }
            catch (XmlException ex)
            {
                throw ExceptionsUtility.HandleXmlException(ex, trackingId);
            }
        }
Example #2
0
        private static T GetModelFromResponse <T>(XmlReader xmlReader, string trackingId) where T : class
        {
            var serializer = new DataContractSerializer(typeof(T));

            try
            {
                using (xmlReader)
                {
                    return((T)serializer.ReadObject(xmlReader));
                }
            }
            catch (SerializationException ex) when(ex.InnerException is XmlException xmlException)
            {
                throw ExceptionsUtility.HandleXmlException(xmlException, trackingId);
            }
        }
Example #3
0
        private async Task <HttpResponseMessage> SendAsync(Func <HttpRequestMessage> generateHttpRequestMessage, CancellationToken cancellationToken)
        {
            var trackingId = Guid.NewGuid().ToString();

            var httpRequestMessage = generateHttpRequestMessage();

            httpRequestMessage.Headers.Add(TrackingIdHeaderKey, trackingId);

            try
            {
                var response = await _httpClient.SendAsync(httpRequestMessage, cancellationToken).ConfigureAwait(false);

                response.Headers.Add(TrackingIdHeaderKey, trackingId);

                if (response.IsSuccessStatusCode)
                {
                    return(response);
                }
                else
                {
                    throw await response.TranslateToMessagingExceptionAsync(trackingId).ConfigureAwait(false);
                }
            }
            catch (HttpRequestException ex)
            {
                var innerException = ex.GetBaseException();
                if (innerException is SocketException socketException)
                {
                    throw ExceptionsUtility.HandleSocketException(socketException, OperationTimeout.Milliseconds, trackingId);
                }
                else
                {
                    throw ExceptionsUtility.HandleUnexpectedException(ex, trackingId);
                }
            }
            catch (XmlException ex)
            {
                throw ExceptionsUtility.HandleXmlException(ex, trackingId);
            }
        }