Пример #1
0
        private bool GetResponse <T>(HttpRequest request, out T data, out RestServiceError error, bool isDynamic)
        {
            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    ISerializer serializer = SerializerFactory.GetSerializer((request.Accept == null || request.Accept.Length == 0) ? ContentType : request.Accept);
                    string      responseTxt;
                    using (StreamReader readStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        responseTxt = readStream.ReadToEnd();
                    }
                    if (!ErrorHandle(out error, responseTxt, serializer))
                    {
                        if (isDynamic)
                        {
                            data = DynamicXml.Parse(responseTxt);
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(responseTxt))
                            {
                                using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(responseTxt)))
                                {
                                    data = (T)serializer.Deserialize(stream, typeof(T));
                                }
                            }
                            else
                            {
                                data = default(T);
                            }
                        }
                        error = null;
                        return(true);
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    HttpWebResponse e = ex.Response as HttpWebResponse;
                    error                   = new RestServiceError();
                    error.StatusCode        = e.StatusCode.GetHashCode();
                    error.StatusDescription = e.StatusDescription;
                    error.Faults            = new List <Error>();
                    error.Faults.Add(new Error()
                    {
                        ErrorCode        = "00000",
                        ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", request.RequestUri.ToString(), ex.ToString())
                    });
                    e.Close();
                }
                else
                {
                    error                   = new RestServiceError();
                    error.StatusCode        = -1;
                    error.StatusDescription = string.Empty;
                    error.Faults            = new List <Error>();
                    error.Faults.Add(new Error()
                    {
                        ErrorCode        = "00000",
                        ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", request.RequestUri.ToString(), ex.ToString())
                    });
                }

                ExceptionHelper.HandleException(ex, string.Format("Call Service {0} Failed.", request.RequestUri.ToString()), new object[0]);
            }
            catch (Exception ex)
            {
                error            = new RestServiceError();
                error.StatusCode = 500;
                error.Faults     = new List <Error>();
                error.Faults.Add(new Error()
                {
                    ErrorCode        = "00000",
                    ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", request.RequestUri.ToString(), ex.ToString())
                });
                ExceptionHelper.HandleException(ex, string.Format("Call Service {0} Failed.", request.RequestUri.ToString()), new object[0]);
            }
            data = default(T);
            return(false);
        }
Пример #2
0
        private void OnGetResponse <T>(IAsyncResult result)
        {
            AsyncArgs <T>    args     = (AsyncArgs <T>)result.AsyncState;
            HttpWebResponse  response = null;
            object           data     = null;
            RestServiceError error    = null;

            try
            {
                response = args.Request.EndGetResponse(result) as HttpWebResponse;
                ISerializer serializer = SerializerFactory.GetSerializer((args.Request.Accept == null || args.Request.Accept.Length == 0) ? ContentType : args.Request.Accept);
                string      responseTxt;
                using (StreamReader readStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    responseTxt = readStream.ReadToEnd();
                }
                if (!ErrorHandle(out error, responseTxt, serializer))
                {
                    if (args.IsDynamic)
                    {
                        data = DynamicXml.Parse(responseTxt);
                    }
                    else
                    {
                        using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(responseTxt)))
                        {
                            data = (T)serializer.Deserialize(stream, typeof(T));
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                HttpWebResponse e = ex.Response as HttpWebResponse;
                error                   = new RestServiceError();
                error.StatusCode        = e.StatusCode.GetHashCode();
                error.StatusDescription = e.StatusDescription;
                error.Faults            = new List <Error>();
                error.Faults.Add(new Error()
                {
                    ErrorCode        = "00000",
                    ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", args.Request.RequestUri.ToString(), ex.ToString())
                });
                e.Close();
                ExceptionHelper.HandleException(ex, string.Format("Call Service {0} Failed.", args.Request.RequestUri.ToString()), new object[0]);
            }
            catch (Exception ex)
            {
                error            = new RestServiceError();
                error.StatusCode = 500;
                error.Faults     = new List <Error>();
                error.Faults.Add(new Error()
                {
                    ErrorCode        = "00000",
                    ErrorDescription = string.Format("Call Service {0} Failed.\r\n\r\nError Detail:{1}", args.Request.RequestUri.ToString(), ex.ToString())
                });
                ExceptionHelper.HandleException(ex, string.Format("Call Service {0} Failed.", args.Request.RequestUri.ToString()), new object[0]);
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
            if (error == null)
            {
                Action <T> handler = args.SucceedHandler;
                if (handler != null)
                {
                    handler((T)data);
                }
            }
            else
            {
                Action <RestServiceError> eHandler = args.ErrorHandler;
                if (eHandler != null)
                {
                    eHandler(error);
                }
            }
        }