Exemple #1
0
        public void OnException(ExceptionContext context)
        {
            try
            {
                var resultValue = ResultBuilder.AsFail(context.Exception, app.ReuqestID);
                context.Result = new JsonResult(resultValue);

                // id为空的是忽略记录日志的接口
                try
                {
                    if (!string.IsNullOrEmpty(app.ReuqestID))
                    {
                        app.RequestInfo.ExceptionLog = "记录请求异常时报错";
                        app.RequestInfo.ExceptionLog = JsonConvert.SerializeObject(resultValue);
                        app.EndRequest();
                    }
                }
                catch { }
            }
            catch { }
        }
Exemple #2
0
        public void OnActionExecuted(ActionExecutedContext context)
        {
            var api = context.ActionDescriptor.AttributeRouteInfo.Template;
            ResultDto <object> resultValue = null;
            ObjectResult       result      = null;
            var isEnableLog = IsEnableLog(context);

            try
            {
                // 异常返回
                if (context.Exception != null)
                {
                    return;
                }
                if (AppManager._proxyAppException != null)
                {
                    var exceptionResult = ResultBuilder.AsFail(AppManager._proxyAppException, app.ReuqestID);
                    AppManager._proxyAppException = null;
                    context.Result = new JsonResult(exceptionResult);
                    return;
                }
                if (app.ExceptionInfo != null)
                {
                    var exceptionResult = ResultBuilder.AsFail(app.ExceptionInfo, app.ReuqestID);
                    context.Result = new JsonResult(exceptionResult);
                    return;
                }

                if (!IsPackageResultDto(context))
                {
                    return;
                }

                result = (context.Result as ObjectResult);
                if (result == null)
                {
                    resultValue = new ResultDto <object>()
                    {
                        Data    = "此接口未定义返回数据,调用结果默认为成功。",
                        Success = true,
                        Message = "操作成功",
                    };
                }
                else
                {
                    resultValue = ResultBuilder.AsSuccess(result.Value);
                }

                resultValue.RequestId = app.ReuqestID;
                if (!string.IsNullOrEmpty(app.Message))
                {
                    resultValue.Message = app.Message;
                }

                context.Result = new JsonResult(resultValue);
                if (isEnableLog)
                {
                    app.LogResponseResult((object)resultValue ?? context.Result);
                }

                // todo 异步
                if (resultValue.Success)
                {
                    //// 推送系统消息
                    //var systemMessage = (context.ActionDescriptor as ControllerActionDescriptor).MethodInfo.GetCustomAttributes(typeof(SystemMessageAttribute), true);
                    //if (systemMessage.Count() == 1)
                    //{
                    //    var message = systemMessage[0] as SystemMessageAttribute;
                    //    message.RelationID = int.Parse(ValueExpression.GetValue(message.RelationIDExpression, app.ActionArguments, resultValue.Data));
                    //    message.ToUserID = int.Parse(ValueExpression.GetValue(message.ToUserIDExpression, app.ActionArguments, resultValue.Data));
                    //    app.PushSystemMessage(message);
                    //}

                    // 记录用户行为
                    var userBehavior = (context.ActionDescriptor as ControllerActionDescriptor).MethodInfo.GetCustomAttributes(typeof(UserBehaviorAttribute), true);
                    if (userBehavior.Count() == 1)
                    {
                        var behavior = userBehavior[0] as UserBehaviorAttribute;
                        behavior.BehaviorValue = ValueExpression.GetValue(behavior.BehaviorValueExpression, app.ActionArguments, resultValue.Data);
                        behavior.RelationID    = int.Parse(ValueExpression.GetValue(behavior.RelationIDExpression, app.ActionArguments, resultValue.Data));
                        app.AddUserBehavior(behavior);
                    }
                }
            }
            catch (Exception ex) { Lib.Log.WriteExceptionLog($"OnActionExecuted:Message:{ex.Message}  <br> StackTrace:{ex.StackTrace}"); }
            finally
            {
                if (isEnableLog)
                {
                    app.EndRequest();
                }
                CleanOnActionClose();
                if (resultValue != null)
                {
                    if (resultValue.Success)
                    {
                        CacheManager.WriteCache(resultValue, context, app.AppID);
                    }
                }
                else if (result != null && result.Value != null)
                {
                    CacheManager.WriteCache(result.Value, context, app.AppID);
                }
            }
        }
Exemple #3
0
        public void OnActionExecuting(ActionExecutingContext context)
        {
            var api         = context.ActionDescriptor.AttributeRouteInfo.Template;
            var isEnableLog = IsEnableLog(context);

            try
            {
                // 设置当前访问信息
                string clientInfo = context.HttpContext.Connection?.RemoteIpAddress.ToString();
                string requestId  = context.HttpContext.Request.Headers["requestId"];
                if (string.IsNullOrEmpty(requestId))
                {
                    requestId = context.HttpContext.TraceIdentifier;
                    context.HttpContext.Request.Headers["requestId"] = requestId;
                }
                app.ActionArguments          = context.ActionArguments;
                AppManager.CurrentAppContext = app;

                // 记录日志
                if (isEnableLog)
                {
                    string parm = JsonConvert.SerializeObject(app.ActionArguments);
                    app.LogRequest(requestId, context.ActionDescriptor.DisplayName, parm, clientInfo, context.ActionDescriptor.AttributeRouteInfo.Template);
                }

                // 读取或设置sessionId和language
                if (IsEnableCookie(context))
                {
                    InitCookies(api);
                }

                // 访问限制,登录等方法不受限制
                if (AuthenticationProxy.IsCheckGateway(context))
                {
                    var clientIp = context.HttpContext.Connection?.RemoteIpAddress.ToString();
                    if (!string.IsNullOrEmpty(clientIp))
                    {
                        GatewayManager.Check(clientIp);
                    }
                }

                var authentication = app.GetService <IAuthenticationProxy>();

                // 鉴权-检查用户token,设置当前用户上下文
                var token = (context.HttpContext.Request.Headers[ApiDocManager.TokenHeaderName]).ToString().Replace("Bearer ", "");
                if (AuthenticationProxy.IsCheckToken(context) || !string.IsNullOrEmpty(token))
                {
                    if (ConfigManager.Configuration["EnableApiDoc"] == "1" && string.IsNullOrEmpty(token))
                    {
                        token = ConfigManager.Configuration["DefaultToken"];
                    }
                    authentication.SetCurrentUser(token, context);
                }
                authentication.SetCurrentGuest(app.SessionID);

                // 检查数据
                VaildateModel(context);

                // 异步
                var asyncTask = AsyncTaskManager.GetTaskInfo(context, app.User?.UserID.ToString());
                if (asyncTask != null)
                {
                    var resultValue = ResultBuilder.AsSuccess(asyncTask);
                    context.Result = new JsonResult(resultValue);
                    return;
                }

                // 从缓存返回
                var cacheResult = CacheManager.ReadCache(context, app);
                if (cacheResult != null)
                {
                    cacheResult.RequestId = context.HttpContext.TraceIdentifier;
                    var cacheTimeRate = CacheTimeSettings.GetCacheTimeRate();
                    var cacheSeconds  = CacheManager.CalcCacheSeconds(context.ActionDescriptor as ControllerActionDescriptor, out var cacheTimeSetting);
                    var seconds       = cacheTimeRate.HttpCacheTime * cacheSeconds;
                    if (seconds < 10)
                    {
                        seconds = 10;
                    }
                    if (seconds > 60)
                    {
                        seconds = 60;
                    }

                    context.HttpContext.Response.GetTypedHeaders().CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
                    {
                        Public = true,
                        MaxAge = TimeSpan.FromSeconds(seconds)
                    };
                    context.Result = new JsonResult(cacheResult);
                    return;
                }
            }
            // 不捕获异常,由ExceptionFilter处理。
            finally
            {
                if (context.Result != null)
                {
                    CleanOnActionClose();
                    if (isEnableLog)
                    {
                        app.LogResponseResult(context.Result);
                        app.EndRequest();
                    }
                }
            }
        }