Exemple #1
0
 public static void IsNotNull(object obj)
 {
     if (obj == null)
     {
         JException.Throw(JErrorMessage.CHECK_OBJECTNULL);
     }
 }
        protected override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
            var        exp   = filterContext.Exception;
            JException myExp = exp as JException;

            sw.Stop();
            LogInfo.Costs   = sw.ElapsedMilliseconds;
            LogInfo.LogType = JLogType.Error.ToString();
            LogHelper.Write(LogInfo, exp);
            if (myExp != null)
            {
                if (Request.IsAjaxRequest())
                {
                    filterContext.Result = Json(new ReturnValueWithTips("error", exp.Source, exp.Message, null), JsonRequestBehavior.AllowGet);
                }
                else
                {
                    throw myExp;
                }
            }
            else if (exp is HttpAntiForgeryException)
            {
                filterContext.Result = Json(new ReturnValueWithTips("warning", "HTTP异常", "正在重新刷新页面", new { Url = Request.UrlReferrer.ToString() }), JsonRequestBehavior.AllowGet);
            }
        }
Exemple #3
0
 public static void IsBoolean(object obj)
 {
     Check.IsNotNull(obj);
     if (!(obj is bool))
     {
         JException.Throw(JErrorMessage.CHECK_OBJECTISNOTBOOLEAN);
     }
 }
Exemple #4
0
        public static void IsArray(object obj)
        {
            //
            // Currently only array type ([]) is supported. Needs to consider support IEnumerable interface later.

            Check.IsNotNull(obj);
            if (!obj.GetType().IsArray)
            {
                JException.Throw(JErrorMessage.CHECK_OBJECTISNOTARRAY);
            }
        }
Exemple #5
0
        private async Task <T> SendRequest <T>(object body, string method)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.SetRawHeader("content-type", "application/json; charset=utf-8");
                request.SetRawHeader("Accept", "application/json");

                request.Method = method;

                if (body != null)
                {
                    string content = JsonConvert.SerializeObject(body);
                    byte[] byte1   = Encoding.ASCII.GetBytes(content);
                    request.ContentLength = byte1.Length;
                    Stream newStream = request.GetRequestStream();
                    newStream.Write(byte1, 0, byte1.Length);
                }


                WebResponse response = request.GetResponse();

                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    string result = await reader.ReadToEndAsync();

                    T objOut = JsonConvert.DeserializeObject <T>(result);
                    return(objOut);
                }
            }
            catch (WebException ex)
            {
                using (StreamReader r = new StreamReader(((HttpWebResponse)ex.Response).GetResponseStream()))
                {
                    string result = await r.ReadToEndAsync();

                    JException jException = JsonConvert.DeserializeObject <JException>(result);
                    throw jException;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                JException jException = new JException();
                jException.message = "Ocurrió un error!";

                throw jException;
            }
        }
Exemple #6
0
        public void Intercept(IInvocation invocation)
        {
            // Create JSON representation for MethodInfo object
            JMethodInfo info = new JMethodInfo(invocation.Method, invocation.Arguments);

            // Serialize the JSON to byte array
            byte[] data = Serializer.SerializeObject(info);

            // Consume service using the injected consumer
            byte[] response = m_Consumer.ConsumeService(data);

            // Get the JSON representation for the server response
            JResponse jResponse = Serializer.DeserializeObject <JResponse>(response);

            switch (jResponse.ResponseType)
            {
            case ResponseType.Empty:
                return;

            case ResponseType.Success:
                // If expected ReturnType is not void, assign the return value
                if (invocation.Method.ReturnType != typeof(void))
                {
                    // Make sure return type is equal to response value type
                    if (invocation.Method.ReturnType != jResponse.ReturnValue.Value.GetType())
                    {
                        // if not, deserialize the value to this type explicitly
                        jResponse.ReturnValue.Value = Serializer.DeserializeObject(jResponse.ReturnValue.Value, invocation.Method.ReturnType);
                    }

                    // Set the return value
                    invocation.ReturnValue = jResponse.ReturnValue.Value;
                }
                break;

            case ResponseType.Fault:
                JException jException = Serializer.DeserializeObject <JException>(jResponse.ReturnValue.Value);
                if (jException != null)
                {
                    throw jException;
                }
                throw new TargetInvocationException(null);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public void Intercept(IInvocation invocation)
        {
            string level = null;

            try
            {
                foreach (Attribute attr in Attribute.GetCustomAttributes(invocation.MethodInvocationTarget))
                {
                    if (attr.GetType() == typeof(TransAttr))
                    {
                        level = ((TransAttr)attr).TransLevel;
                    }
                }

                if (TRANS_LEVEL.MANDATORY == level)
                {
                    ConnectorFactory.ProceedInTransaction(invocation, connId);
                }
                else
                {
                    invocation.Proceed();
                }
            }
            catch (JException je)
            {
                throw je;
            }
            catch (Exception e)
            {
                string mthod = invocation.Method.Name;
                //记录日志
                Trace.TraceError("【" + mgrNm + "." + mthod + "】 " + e.StackTrace);
                JException je = new JException();
                je.JCode = "JE-000";
                throw je;
            }
        }