Esempio n. 1
0
        /// <summary>
        /// 协议错误
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        private string ProtocolError(WebException exception, ref WebExceptionStatus status)
        {
            try
            {
                var codes = exception.Message.Split(new[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
                if (codes.Length == 3)
                {
                    if (int.TryParse(codes[1], out var s))
                    {
                        switch (s)
                        {
                        case 404:
                            return(ToErrorString(ErrorCode.NetworkError, "页面不存在", "页面不存在"));

                        case 503:
                            return(ToErrorString(ErrorCode.NetworkError, "拒绝访问", "页面不存在"));
                            //default:
                            //    return ToErrorString(ErrorCode.NetworkError, $"错误{s}", exception.Message);
                        }
                    }
                }
                var msg = ReadResponse(exception.Response);
                RuntimeWaring.Waring(Host, ApiName, msg);
                return(msg); //ToErrorString(ErrorCode.NetworkError, "未知错误", );
            }
            catch (Exception e)
            {
                status = WebExceptionStatus.UnknownError;
                LogRecorder.Exception(e);
                return(ToErrorString(ErrorCode.NetworkError, "未知错误", e.Message));
            }
        }
Esempio n. 2
0
 /// <summary>
 /// 初始化
 /// </summary>
 public static void Flush()
 {
     AppConfig.Initialize();
     RouteChahe.Flush();
     RuntimeWaring.Flush();
     ZeroFlush();
     //Datas = new List<RouteData>();
 }
Esempio n. 3
0
        /// <summary>
        /// 开始计数
        /// </summary>
        /// <returns></returns>
        public void End(RouteData data)
        {
            data.End = DateTime.Now;
            try
            {
                var tm = (DateTime.Now - Start).TotalMilliseconds;
                if (tm > 200)
                {
                    LogRecorder.Warning($"{data.HostName}/{data.ApiName}:执行时间异常({tm:F2}ms):");
                }

                if (tm > AppConfig.Config.SystemConfig.WaringTime)
                {
                    RuntimeWaring.Waring(data.HostName, data.ApiName, $"执行时间异常({tm:F0}ms)");
                }

                long unit = DateTime.Today.Year * 1000000 + DateTime.Today.Month * 10000 + DateTime.Today.Day * 100 + DateTime.Now.Hour;
                if (unit != Unit)
                {
                    Unit = unit;
                    Save();
                    Station = new CountItem();
                }

                Station.SetValue(tm, data);
                if (string.IsNullOrWhiteSpace(data.HostName))
                {
                    return;
                }
                CountItem host;
                lock (Station)
                {
                    if (!Station.Items.TryGetValue(data.HostName, out host))
                    {
                        Station.Items.Add(data.HostName, host = new CountItem());
                    }
                }
                host.SetValue(tm, data);

                if (string.IsNullOrWhiteSpace(data.ApiName))
                {
                    return;
                }
                CountItem api;
                lock (host)
                {
                    if (!host.Items.TryGetValue(data.ApiName, out api))
                    {
                        host.Items.Add(data.ApiName, api = new CountItem());
                    }
                }
                api.SetValue(tm, data);
            }
            catch (Exception e)
            {
                LogRecorder.Exception(e);
            }
        }
Esempio n. 4
0
 /// <summary>
 /// 初始化
 /// </summary>
 public static void Initialize()
 {
     // 日志支持
     //Agebull.Common.Logging.LogRecorder.GetRequestIdFunc = () => ApiContext.RequestContext.RequestId;.
     LogRecorder.Initialize(new RemoteRecorder());
     AppConfig.Initialize(Path.Combine(Startup.Configuration["contentRoot"], "route_config.json"));
     StationProgram.Run();
     RouteChahe.Flush();
     RuntimeWaring.Flush();
     RouteCommand.ZeroFlush();
     //Datas = new List<RouteData>();
 }
Esempio n. 5
0
        /// <summary>
        /// 检查返回值是否合理
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        internal static bool CheckResult(RouteData data)
        {
            if (data.Status != RouteStatus.None || data.HostName == null)// "".Equals(data.HostName,StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }
            try
            {
                var result = JsonConvert.DeserializeObject <ApiResult>(data.ResultMessage);
                if (result == null)
                {
                    RuntimeWaring.Waring(data.HostName, data.ApiName, "返回值非法(空内容)");
                    return(false);
                }
                if (result.Status != null && !result.Result)
                {
                    switch (result.Status.ErrorCode)
                    {
                    case ErrorCode.ReTry:
                    case ErrorCode.DenyAccess:
                    case ErrorCode.Ignore:
                    case ErrorCode.ArgumentError:
                    case ErrorCode.Auth_RefreshToken_Unknow:
                    case ErrorCode.Auth_ServiceKey_Unknow:
                    case ErrorCode.Auth_AccessToken_Unknow:
                    case ErrorCode.Auth_User_Unknow:
                    case ErrorCode.Auth_Device_Unknow:
                    case ErrorCode.Auth_AccessToken_TimeOut:
                        return(false);

                    default:
                        RuntimeWaring.Waring(data.HostName, data.ApiName, result.Status?.Message ?? "处理错误但无消息");
                        return(false);
                    }
                }
            }
            catch
            {
                RuntimeWaring.Waring(data.HostName, data.ApiName, "返回值非法(不是Json格式)");
                return(false);
            }
            return(true);
        }
Esempio n. 6
0
        /// <summary>
        /// 调用
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static void CallTask(HttpContext context)
        {
            var uri = context.Request.GetUri();

            try
            {
                HttpProtocol.FormatResponse(context.Response);
                //内容页转向
                if (uri.LocalPath.IndexOf(".", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    context.Response.Redirect(AppConfig.Config.SystemConfig.ContextHost + uri.LocalPath.Trim('/'));
                    return;
                }
                //跨域支持
                if (context.Request.Method.ToUpper() == "OPTIONS")
                {
                    HttpProtocol.Cros(context.Response);
                    return;
                }
                //命令
                if (RouteCommand.InnerCommand(uri.LocalPath, context.Response))
                {
                    return;
                }
            }
            catch (Exception e)
            {
                LogRecorder.Exception(e);
                RuntimeWaring.Waring("Route", uri.LocalPath, e.Message);
                context.Response.WriteAsync(RouteRuntime.InnerError, Encoding.UTF8);
                return;
            }

            var router = new HttpRouter(context);

            HttpIoLog.OnBegin(router.Data);
            var counter = PerformanceCounter.OnBegin(router.Data);

            try
            {
                var checker = new SecurityChecker
                {
                    Request = context.Request
                };
                if (!checker.PreCheck())
                {
                    router.Data.Status = RouteStatus.DenyAccess;
                    context.Response.WriteAsync(RouteRuntime.Inner2Error, Encoding.UTF8);
                }
                else
                {
                    // 正常调用
                    router.Call();
                    LogRecorder.BeginStepMonitor("End");
                    // 写入返回
                    router.WriteResult();
                    // 缓存
                    RouteChahe.CacheResult(router.Data);
                }
            }
            catch (Exception e)
            {
                router.Data.Status = RouteStatus.LocalError;
                LogRecorder.Exception(e);
                RuntimeWaring.Waring("Route", uri.LocalPath, e.Message);
                context.Response.WriteAsync(RouteRuntime.InnerError, Encoding.UTF8);
            }
            finally
            {
                //计时
                counter.End(router.Data);
                HttpIoLog.OnEnd(router.Data);
            }
        }