private static Task <WebServiceException> CreateExceptionAsync(WebServiceResponseHandlerInfo info, string message)
 {
     if (info.IsContentRead)
     {
         return(Task.FromResult(HttpResponseMessageUtility.CreateWebServiceException(info.WebResponse, message)));
     }
     else
     {
         info.MarkContentAsRead();
         return(HttpResponseMessageUtility.CreateWebServiceExceptionWithContentPreviewAsync(info.WebResponse, message));
     }
 }
        private async Task <object> ReadContentAsAsync(WebServiceResponseHandlerInfo <TResponse> info, Type propertyType)
        {
            HttpResponseMessage webResponse = info.WebResponse;
            object content = null;

            if (propertyType == typeof(bool) || propertyType == typeof(bool?))
            {
                content = true;
            }
            else if (propertyType == typeof(string))
            {
                info.MarkContentAsRead();
                content = await webResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
            }
            else if (propertyType == typeof(byte[]))
            {
                info.MarkContentAsRead();
                content = await webResponse.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
            }
            else if (propertyType == typeof(Stream))
            {
                info.MarkContentAsRead();
                content = await WebResponseStream.CreateAsync(info.WebResponse).ConfigureAwait(false);
            }
            else if (webResponse.HasJson() || webResponse.Content.Headers.ContentType == null)
            {
                info.MarkContentAsRead();
                content = await webResponse.GetJsonAsAsync(propertyType, JsonSettings).ConfigureAwait(false);
            }

            if (content == null)
            {
                throw await CreateExceptionAsync(info, "Web response content cannot be read as {0}.".FormatInvariant(propertyType)).ConfigureAwait(false);
            }

            return(content);
        }
Пример #3
0
        /// <summary>
        /// Called when the response has been handled, i.e. immediately before it is returned.
        /// </summary>
        /// <param name="info">The web service response handler information.</param>
        protected virtual async Task OnResponseHandledCoreAsync(WebServiceResponseHandlerInfo info)
        {
            m_requestMethod         = info.WebResponse !.RequestMessage.Method.Method;
            m_requestUri            = info.WebResponse.RequestMessage.RequestUri;
            m_responseStatusCode    = info.WebResponse.StatusCode;
            m_responseHeaders       = info.WebResponse.Headers;
            m_responseContentType   = info.WebResponse.Content?.Headers?.ContentType?.ToString();
            m_responseContentLength = info.WebResponse.Content?.Headers?.ContentLength;

            if (!info.IsContentRead)
            {
                m_responseContentPreview = await HttpResponseMessageUtility.ReadContentPreviewAsync(info.WebResponse).ConfigureAwait(false);

                info.MarkContentAsRead();
            }
        }