예제 #1
0
 private static List <DataTable> GetData(string data)
 {
     if (data == null)
     {
         return(new List <DataTable>(0));
     }
     else
     {
         List <DataTable> list = new List <DataTable>();
         var obj = DynamicXml.Parse(data);
         if (obj.TotalCount == 0)
         {
             return(new List <DataTable>(0));
         }
         if (obj.IsArray)
         {
             bool isQueryResultList = false;
             foreach (var item in obj)
             {
                 isQueryResultList = item.IsDefined("TotalCount") && item.IsDefined("Rows");
                 break;
             }
             if (isQueryResultList) // QueryResultList
             {
                 foreach (var item in obj)
                 {
                     list.Add(ConvertToDataTable(item.Rows));
                 }
             }
             else // DataTable
             {
                 list.Add(ConvertToDataTable(obj));
             }
         }
         else // QueryResult
         {
             list.Add(ConvertToDataTable(obj.Rows));
         }
         return(list);
     }
 }
예제 #2
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)
     {
         HttpWebResponse e = ex.Response as HttpWebResponse;
         if (e != null)
         {
             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 if (ex.Response != null)
         {
             error                   = new RestServiceError();
             error.StatusCode        = -1;
             error.StatusDescription = "未知错误,返回的WebException.Response类型为:" + ex.Response.GetType();
             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())
             });
         }
         else
         {
             error                   = new RestServiceError();
             error.StatusCode        = -2;
             error.StatusDescription = "未知错误,返回的WebException.Response为null。";
             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);
 }
예제 #3
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);
                }
            }
        }