Exemple #1
0
        private bool ErrorHandle(out RestServiceError error, string responseTxt, ISerializer serializer)
        {
            bool existError = false;

            error = null;
            if (responseTxt != null)
            {
                try
                {
                    using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(responseTxt)))
                    {
                        error = serializer.Deserialize(stream, typeof(RestServiceError)) as RestServiceError;
                    }
                    if (!(existError = (error != null && error.Faults != null && error.StatusCode > 0)))
                    {
                        error = null;
                    }
                }
                catch
                {
                    existError = false;
                }
            }

            return(existError);
        }
Exemple #2
0
        private bool HttpGetData <T>(string relativeUrl, out T returnData, out RestServiceError error, bool isDynamic = false)
        {
            string url = CombineUrl(this.m_ServicePath, relativeUrl == null || relativeUrl.Trim().Length <= 0 ? string.Empty : relativeUrl);

            url = HandleGetRequestUrl(url);
            HttpRequest request = new HttpRequest(url, ContentType, ContentType, Timeout);

            request.Method = Operating.GET;
            return(GetResponse(request, out returnData, out error, isDynamic));
        }
Exemple #3
0
        private bool ErrorHandle(out RestServiceError error, HttpWebResponse response, ISerializer serializer)
        {
            bool existError = false;

            error = null;
            if (response != null)
            {
                try
                {
                    error = serializer.Deserialize(response.GetResponseStream(), typeof(RestServiceError)) as RestServiceError;
                    if (!(existError = (error != null && error.Faults != null && error.StatusCode > 0)))
                    {
                        error = null;
                    }
                }
                catch
                {
                    existError = false;
                }
            }

            return(existError);
        }
Exemple #4
0
        public bool Delete(string relativeUrl, object data, out RestServiceError error)
        {
            object returnData;

            return(Delete <object>(relativeUrl, data, out returnData, out error));
        }
Exemple #5
0
 public bool Delete <T>(string relativeUrl, object data, out T returnData, out RestServiceError error)
 {
     return(HttpPostData <T>(Operating.DELETE, relativeUrl, data, out returnData, out error));
 }
Exemple #6
0
 public bool Query <T>(string relativeUrl, object condition, out T returnData, out RestServiceError error)
 {
     return(HttpPostData <T>(Operating.POST, relativeUrl, condition, out returnData, out error));
 }
Exemple #7
0
 public bool Query <T>(string relativeUrl, out T returnData, out RestServiceError error)
 {
     return(HttpGetData <T>(relativeUrl, out returnData, out error));
 }
Exemple #8
0
 public bool QueryDynamicData(string relativeUrl, object condition, out dynamic returnData, out RestServiceError error)
 {
     return(HttpPostData <dynamic>(Operating.POST, relativeUrl, condition, out returnData, out error, true));
 }
Exemple #9
0
 public bool QueryDynamicData(string relativeUrl, out dynamic returnData, out RestServiceError error)
 {
     return(HttpGetData <dynamic>(relativeUrl, out returnData, out error, true));
 }
Exemple #10
0
        private bool HttpPostData <T>(string operating, string relativeUrl, object data, out T returnData, out RestServiceError error, bool isDynamic = false)
        {
            string      url     = CombineUrl(this.m_ServicePath, relativeUrl == null || relativeUrl.Trim().Length <= 0 ? string.Empty : relativeUrl);
            HttpRequest request = new HttpRequest(url, ContentType, ContentType, Timeout);

            HandlePostRequestHeader(request, operating);
            request.Method = Operating.POST;
            if (data != null)
            {
                ISerializer serializer = SerializerFactory.GetSerializer(request.ContentType);
                if (serializer != null)
                {
                    string postContent = serializer.Serialization(data, data.GetType());
                    using (StreamWriter stream = new StreamWriter(request.GetRequestStream()))
                    {
                        stream.Write(postContent);
                    }
                }
            }
            return(GetResponse(request, out returnData, out error, isDynamic));
        }
Exemple #11
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);
        }
Exemple #12
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);
                }
            }
        }