Exemple #1
0
 public static void RegisterSerializer(string serializerName, ISerializer serializer)
 {
     SerializerFactory.Register(serializerName, serializer);
 }
Exemple #2
0
        private void OnGetResponse(IAsyncResult result)
        {
            AsyncArgs        args     = result.AsyncState as AsyncArgs;
            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) ? args.UserState.ToString() : args.Request.Accept);

                if (serializer != null)
                {
                    if (!ErrorHandle(out error, response, serializer))
                    {
                        data = serializer.Deserialize(response.GetResponseStream(), args.DataType);
                    }
                }
            }
            catch (WebException ex)
            {
                //HttpWebResponse e = ex.Response as HttpWebResponse;
                //error = new RestServiceError();
                //error.StatusCode = e.StatusCode.GetHashCode();
                //error.StatusDescription = e.StatusDescription;
                //error.Faults = new System.Collections.ObjectModel.ObservableCollection<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();
                HttpWebResponse e = ex.Response as HttpWebResponse;
                if (e != null)
                {
                    error                   = new RestServiceError();
                    error.StatusCode        = e.StatusCode.GetHashCode();
                    error.StatusDescription = e.StatusDescription;
                    error.Faults            = new System.Collections.ObjectModel.ObservableCollection <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();
                }
                else if (ex.Response != null)
                {
                    error                   = new RestServiceError();
                    error.StatusCode        = -1;
                    error.StatusDescription = "未知错误,返回的WebException.Response类型为:" + ex.Response.GetType();
                    error.Faults            = new System.Collections.ObjectModel.ObservableCollection <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())
                    });
                }
                else
                {
                    error                   = new RestServiceError();
                    error.StatusCode        = -2;
                    error.StatusDescription = "未知错误,返回的WebException.Response为null。";
                    error.Faults            = new System.Collections.ObjectModel.ObservableCollection <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())
                    });
                }
                ComponentFactory.Logger.LogError(ex, string.Format("Call Service {0} Failed.", args.Request.RequestUri.ToString()), null);
            }
            catch (Exception ex)
            {
                error            = new RestServiceError();
                error.StatusCode = 500;
                error.Faults     = new System.Collections.ObjectModel.ObservableCollection <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())
                });
                ComponentFactory.Logger.LogError(ex, string.Format("Call Service {0} Failed.", args.Request.RequestUri.ToString()), null);
            }
            finally
            {
                DisplayLoading(false);
                if (response != null)
                {
                    response.Close();
                }
            }
            CallBackHandle(data, error, args);
        }
Exemple #3
0
        public static void ExportFile(this RestClient restClient, string relativeUrl, string exporterName, TextInfo[] textInfoList, ColumnSet[] columns = null)
        {
            ForwardRequestData data = new ForwardRequestData();

            if (textInfoList != null && textInfoList.Length > 0)
            {
                data.TextInfoList = new List <TextInfo>(textInfoList);
            }
            if (columns != null)
            {
                data.ColumnSetting = new List <List <ColumnData> >(columns.Length);
                foreach (var d in columns)
                {
                    data.ColumnSetting.Add(d.GetColumnDataList());
                }
            }
            data.ExporterName = exporterName;
            if (relativeUrl.ToLower().StartsWith("http"))
            {
                data.Url = relativeUrl;
            }
            else
            {
                Uri u = new Uri(restClient.ServicePath);
                data.Port = u.Port;
                data.Url  = u.AbsolutePath.TrimEnd(new char[] { '/', '\\' }) + "/" + relativeUrl.TrimStart(new char[] { '/', '\\' });
                if (u.Query.Length > 0)
                {
                    data.Url = data.Url + u.Query;
                }
            }
            data.HttpContentType = restClient.ContentType;
            data.HttpMethod      = Operating.GET;
            string hour      = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now).Hours.ToString();
            string userSysNo = CPApplication.Current.LoginUser.UserSysNo.GetValueOrDefault().ToString();
            string userAcct  = CPApplication.Current.LoginUser.LoginName;
            string sign      = restClient.SignParameters(userSysNo, userAcct, hour);

            data.Parameters = new List <CodeNamePair>
            {
                new CodeNamePair {
                    Code = "Portal_Accept", Name = restClient.ContentType
                },
                new CodeNamePair {
                    Code = "Portal_Language", Name = Thread.CurrentThread.CurrentUICulture.Name
                },
                new CodeNamePair {
                    Code = "Portal_UserSysNo", Name = userSysNo
                },
                new CodeNamePair {
                    Code = "Portal_UserAcct", Name = userAcct
                },
                new CodeNamePair {
                    Code = "Portal_TimeZone", Name = hour
                },
                new CodeNamePair {
                    Code = "Portal_Sign", Name = sign
                }
            };

            restClient.Query <FileExportResult>(s_RelativeUrl, data, (obj, args) =>
            {
                if (args.FaultsHandle())
                {
                    return;
                }

                if (args.Result.RestServiceError != null && args.Result.RestServiceError.Trim().Length > 0)
                {
                    ISerializer serializer = SerializerFactory.GetSerializer(restClient.ContentType);
                    if (serializer != null)
                    {
                        RestServiceError error = null;
                        using (MemoryStream me = new MemoryStream(Encoding.UTF8.GetBytes(args.Result.RestServiceError)))
                        {
                            error = serializer.Deserialize(me, typeof(RestServiceError)) as RestServiceError;
                        }
                        args.Error = error;
                    }
                }
                if (args.FaultsHandle())
                {
                    return;
                }
                string url = restClient.ServicePath.TrimEnd(new char[] { '/', '\\' }) + "/" + args.Result.DownloadUrl.TrimStart(new char[] { '/', '\\' });
                UtilityHelper.OpenWebPage(url);
            });
        }