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));
     }
 }
예제 #2
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();
            }
        }
        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);
        }
        /// <summary>
        /// Sets Headers property.
        /// </summary>
        /// <param name="info">The web service response handler information.</param>
        protected override async Task OnResponseHandledCoreAsync(WebServiceResponseHandlerInfo info)
        {
            await base.OnResponseHandledCoreAsync(info).ConfigureAwait(false);

            Headers = info.WebResponse !.Headers;
        }
        /// <summary>
        /// Overrides HandleResponseCore.
        /// </summary>
        protected override async Task <bool> HandleResponseCoreAsync(WebServiceResponseHandlerInfo <TResponse> info)
        {
            TResponse           response     = CreateResponse();
            Type                responseType = response.GetType();
            HttpResponseMessage webResponse  = info.WebResponse;

            // find specific status code property
            bool           isStatusCodeHandled = false;
            HttpStatusCode statusCode          = webResponse.StatusCode;
            string         statusCodeText      = statusCode.ToString();
            PropertyInfo   resultProperty      = GetProperty(responseType, statusCodeText);
            object         content             = null;

            if (resultProperty != null && resultProperty.CanWrite)
            {
                Type resultPropertyType = resultProperty.PropertyType;
                content = await ReadContentAsAsync(info, resultPropertyType).ConfigureAwait(false);

                resultProperty.SetValue(response, content, null);
                isStatusCodeHandled = true;
            }

            PropertyInfo statusCodeProperty = GetProperty(responseType, "StatusCode");

            if (statusCodeProperty != null && statusCodeProperty.CanWrite)
            {
                Type statusCodePropertyType = statusCodeProperty.PropertyType;
                if (statusCodePropertyType == typeof(HttpStatusCode))
                {
                    statusCodeProperty.SetValue(response, webResponse.StatusCode, null);
                }
                else if (statusCodePropertyType == typeof(int))
                {
                    statusCodeProperty.SetValue(response, (int)webResponse.StatusCode, null);
                }
                else
                {
                    throw await CreateExceptionAsync(info, "Web response status code cannot be read as {0}.".FormatInvariant(statusCodePropertyType));
                }

                isStatusCodeHandled = true;
            }

            // make sure status code is handled
            if (!isStatusCodeHandled)
            {
                throw await CreateExceptionAsync(info, "Status code not handled.").ConfigureAwait(false);
            }

            // read headers
            foreach (var header in webResponse.Headers)
            {
                string headerName = header.Key;
                // remove hyphens before looking for property setter by name
                string       propertyName   = headerName.Replace("-", "");
                PropertyInfo headerProperty = GetProperty(responseType, propertyName);
                if (headerProperty != null && headerProperty.CanWrite)
                {
                    // get header text
                    string headerText = header.Value.Join("; ");

                    // convert header text to supported types
                    Type headerPropertyType = headerProperty.PropertyType;
                    if (headerPropertyType == typeof(string))
                    {
                        headerProperty.SetValue(response, headerText, null);
                    }
                    else if (headerPropertyType == typeof(int) || headerPropertyType == typeof(int?))
                    {
                        headerProperty.SetValue(response, InvariantConvert.ParseInt32(headerText), null);
                    }
                    else if (headerPropertyType == typeof(long) || headerPropertyType == typeof(long?))
                    {
                        headerProperty.SetValue(response, InvariantConvert.ParseInt64(headerText), null);
                    }
                    else if (headerPropertyType == typeof(Uri))
                    {
                        headerProperty.SetValue(response, new Uri(webResponse.RequestMessage.RequestUri, headerText), null);
                    }
                    else if (headerPropertyType == typeof(DateTime) || headerPropertyType == typeof(DateTime?))
                    {
                        headerProperty.SetValue(response, DateTime.ParseExact(headerText, "R", CultureInfo.InvariantCulture), null);
                    }
                    else if (headerPropertyType == typeof(byte[]))
                    {
                        headerProperty.SetValue(response, Convert.FromBase64String(headerText), null);
                    }
                    else
                    {
                        throw await CreateExceptionAsync(info, "Web response header cannot be read as {0}. {1}: {2}".FormatInvariant(headerPropertyType, headerName, headerText)).ConfigureAwait(false);
                    }
                }
            }

            // allow response to read extra data
            AutoWebServiceResponse autoWebServiceResponse = response as AutoWebServiceResponse;

            if (autoWebServiceResponse != null)
            {
                await autoWebServiceResponse.OnResponseHandledAsync(info).ConfigureAwait(false);
            }

            // detach response if necessary
            if (content is WebResponseStream)
            {
                info.DetachWebResponse();
            }

            // success
            info.Response = response;
            return(true);
        }
예제 #6
0
 internal Task OnResponseHandledAsync(WebServiceResponseHandlerInfo info)
 {
     return(OnResponseHandledCoreAsync(info));
 }