Example #1
0
        public void GenericExceptionWithNumberParameterTest()
        {
            var e = new GenericException(GenericException.NullError, "It went wrong").With("Count", 5);

            Assert.IsTrue(e.Message == "It went wrong");
            Assert.IsTrue(e.ToString() == "It went wrong... Count: 5");
        }
Example #2
0
        public void GenericExceptionWithSingleParameterTest()
        {
            var e = new GenericException(GenericException.NullError, "It went wrong").With("File", "c:/home.txt");

            Assert.IsTrue(e.Message == "It went wrong");
            Assert.IsTrue(e.ToString() == "It went wrong... File: c:/home.txt");
        }
Example #3
0
        public void GenericExceptionWithTwoParametersTest()
        {
            var e = new GenericException(GenericException.NullError, "It went wrong").With("File", "c:/home.txt").With("Next", "Fred");

            Assert.IsTrue(e.Message == "It went wrong");
            Assert.IsTrue(e.ToString() == "It went wrong... File: c:/home.txt; Next: Fred" || e.ToString() == "It went wrong.. Next: Fred; File: c:/home.txt");
        }
Example #4
0
        public void GenericExceptionBasicExceptionTest()
        {
            var e = new GenericException(GenericException.NullError, "It went wrong");

            Assert.IsTrue(e.Id == GenericException.NullError);
            Assert.IsTrue(e.Message == "It went wrong");
            Assert.IsTrue(e.ToString() == "It went wrong");
        }
        /// <summary>处理请求</summary>
        public static string ProcessRequest(HttpContextBase context, string methodName, string rawInput, ILog logger, APIInvokeDelegate methodInvoke)
        {
            // 请求响应的内容
            string responseText = string.Empty;
            // clientId
            string clientId = RequestHelper.Fetch("clientId", new string[] { "client_id", "app_key", "appKey" });
            // accessToken
            string accessToken = RequestHelper.Fetch("accessToken", "access_token");
            // 默认支持 form-data 方式
            string xml = (context.Request.Form["xhr-xml"] == null) ? string.Empty : context.Request.Form["xhr-xml"];

            // 支持 application/xml 请求方式
            if (context.Request.ContentType.IndexOf("application/xml") > -1 && string.IsNullOrEmpty(xml) && !string.IsNullOrEmpty(rawInput))
            {
                xml = rawInput;
            }

            // 支持 application/json 请求方式
            if (context.Request.ContentType.IndexOf("application/json") > -1 && string.IsNullOrEmpty(xml) && !string.IsNullOrEmpty(rawInput))
            {
                XmlDocument xmlDoc = JsonHelper.ToXmlDocument(rawInput);

                xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<request>" + xmlDoc.DocumentElement.InnerXml + "</request>";
            }

            if (!string.IsNullOrEmpty(xml) || context.Request.QueryString.Count > 0 || (context.Request.HttpMethod == "POST" && context.Request.Form.Count > 0))
            {
                XmlDocument doc = new XmlDocument();

                if (string.IsNullOrEmpty(xml))
                {
                    doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<request></request>");
                }
                else
                {
                    doc.LoadXml(xml);
                }

                // 将 QueryString 中,除 xhr-name 外的所有参数转为统一的Xml文档的数据
                if (context.Request.QueryString.Count > 0)
                {
                    for (int i = 0; i < context.Request.QueryString.Count; i++)
                    {
                        if (string.IsNullOrEmpty(context.Request.QueryString.Keys[i]))
                        {
                            continue;
                        }

                        if (context.Request.QueryString.Keys[i] != "xhr-name" && context.Request.QueryString.Keys[i].IndexOf("[") == -1)
                        {
                            XmlElement element = doc.CreateElement(context.Request.QueryString.Keys[i]);

                            element.InnerText = context.Request.QueryString[i];

                            doc.DocumentElement.AppendChild(element);
                        }
                    }

                    doc = AnalyzePagingXml(doc, context.Request.QueryString);

                    doc = AnalyzeQueryXml(doc, context.Request.QueryString);
                }

                // 将表单中,除 xhr-name 和 xhr-xml 外的所有参数转为统一的Xml文档的数据
                if (context.Request.HttpMethod == "POST" && context.Request.Form.Count > 0)
                {
                    for (int i = 0; i < context.Request.Form.Count; i++)
                    {
                        if (string.IsNullOrEmpty(context.Request.Form.Keys[i]))
                        {
                            continue;
                        }

                        if (context.Request.Form.Keys[i] != "xhr-name" && context.Request.Form.Keys[i] != "xhr-xml" && context.Request.Form.Keys[i].IndexOf("[") == -1)
                        {
                            XmlElement element = doc.CreateElement(context.Request.Form.Keys[i]);

                            element.InnerText = context.Request.Form[i];

                            doc.DocumentElement.AppendChild(element);
                        }
                    }

                    doc = AnalyzeQueryXml(doc, context.Request.QueryString);
                }

                string clientTargetObject = XmlHelper.Fetch("clientTargetObject", doc);

                try
                {
                    // 记录
                    if (ConnectConfigurationView.Instance.EnableCallLog == "ON")
                    {
                        ConnectCallInfo call = new ConnectCallInfo(clientId, context.Request.RawUrl, string.IsNullOrEmpty(rawInput) ? doc.InnerXml : rawInput);

                        call.AccessToken = accessToken;

                        try
                        {
                            call.Start();

                            var responseObject = methodInvoke(methodName, doc, logger);

                            responseText = call.ResponseData = (responseObject == null) ? string.Empty : responseObject.ToString();

                            if (call.RequestData.Length > 2048)
                            {
                                call.RequestData = "[Long String] " + call.RequestData.Length;
                            }

                            if (call.ResponseData.Length > 2048)
                            {
                                call.ResponseData = "[Long String] " + call.ResponseData.Length;
                            }

                            call.ReturnCode = 0;
                        }
                        catch
                        {
                            call.ReturnCode = 1;

                            throw;
                        }
                        finally
                        {
                            call.Finish();

                            call.IP = IPQueryContext.GetClientIP();

                            ConnectContext.Instance.ConnectCallService.Save(call);
                        }
                    }
                    else
                    {
                        var responseObject = methodInvoke(methodName, doc, logger);

                        responseText = (responseObject == null) ? string.Empty : responseObject.ToString();
                    }

                    if (responseText.IndexOf("\"message\":") > -1 &&
                        !string.IsNullOrEmpty(clientTargetObject))
                    {
                        responseText = responseText.Insert(responseText.IndexOf("\"message\":"), "\"clientTargetObject\":\"" + clientTargetObject + "\",");
                    }
                }
                catch (GenericException genericException)
                {
                    responseText = genericException.ToString();
                }
                catch (ThreadAbortException threadAbortException)
                {
                    GenericException exception = new GenericException("9999", threadAbortException);

                    responseText = exception.ToString();
                }
                catch (Exception ex)
                {
                    GenericException exception = null;

                    if (ex.InnerException is GenericException)
                    {
                        exception = (GenericException)ex.InnerException;
                    }
                    else
                    {
                        exception = new GenericException("-1", ex);
                    }

                    responseText = exception.ToString();
                }
            }

            // JSONP
            string callback = context.Request["callback"];

            return(string.IsNullOrEmpty(callback)
                ? responseText
                : callback + "(" + responseText + ")");
        }