public void OnException(ExceptionContext filterContext)
 {
     if (filterContext.Exception is ApiExecuteException)
     {
     }
     else if (filterContext.Exception is RemoteException)
     {
     }
     filterContext.ExceptionHandled = true;
 }
 protected override void OnException(ExceptionContext filterContext)
 {
     if (filterContext.Exception is ApiExecuteException)
     {
     }
     else if (filterContext.Exception is RemoteException)
     {
     }
     filterContext.ExceptionHandled = true;
 }
 protected override void OnException(ExceptionContext filterContext)
 {
     // 标记已处理
     filterContext.ExceptionHandled = true;
 }
        /// <summary>
        /// 调用自身实现的Api行为
        /// 将返回值发送给客户端        
        /// </summary>       
        /// <param name="actionContext">上下文</param>       
        /// <param name="filters">过滤器</param>
        /// <returns>当正常执行输出Api的结果时返回true</returns>
        private bool ExecuteAction(ActionContext actionContext, IEnumerable<IFilter> filters)
        {
            var action = actionContext.Action;
            var packet = actionContext.Packet;
            var session = actionContext.Session;
            var serializer = this.Server.Serializer;
            var parameters = Common.GetAndUpdateParameterValues(serializer, actionContext);

            // Api执行前
            this.ExecFiltersBeforeAction(filters, actionContext);
            if (actionContext.Result != null)
            {
                var exceptionContext = new ExceptionContext(actionContext, actionContext.Result);
                Common.SendRemoteException(actionContext.Session.UnWrap(), exceptionContext);
                return false;
            }

            // 执行Api            
            var apiResult = action.Execute(this, parameters);

            // Api执行后
            this.ExecFiltersAfterAction(filters, actionContext);
            if (actionContext.Result != null)
            {
                var exceptionContext = new ExceptionContext(actionContext, actionContext.Result);
                Common.SendRemoteException(actionContext.Session.UnWrap(), exceptionContext);
                return false;
            }

            // 返回数据
            if (action.IsVoidReturn == false && session.IsConnected)
            {
                packet.Body = serializer.Serialize(apiResult);
                session.UnWrap().Send(packet.ToByteRange());
            }
            return true;
        }
 /// <summary>
 /// 处理Api行为执行过程中产生的异常
 /// </summary>
 /// <param name="actionContext">上下文</param>
 /// <param name="filters">过滤器</param>
 /// <param name="exception">异常项</param>
 private void ProcessExecutingException(ActionContext actionContext, IEnumerable<IFilter> filters, Exception exception)
 {
     var exceptionContext = new ExceptionContext(actionContext, new ApiExecuteException(exception));
     Common.SendRemoteException(actionContext.Session.UnWrap(), exceptionContext);
     this.ExecAllExceptionFilters(filters, exceptionContext);
 }
        /// <summary>
        /// 执行异常过滤器
        /// </summary>       
        /// <param name="filters">Api行为过滤器</param>
        /// <param name="exceptionContext">上下文</param>       
        private void ExecAllExceptionFilters(IEnumerable<IFilter> filters, ExceptionContext exceptionContext)
        {
            var totalFilters = this.GetTotalFilters(filters);
            foreach (var filter in totalFilters)
            {
                filter.OnException(exceptionContext);
                if (exceptionContext.ExceptionHandled == true) break;
            }

            if (exceptionContext.ExceptionHandled == false)
            {
                throw exceptionContext.Exception;
            }
        }
        /// <summary>
        /// 执行异常过滤器
        /// </summary>         
        /// <param name="exceptionContext">上下文</param>       
        private void ExecGlobalExceptionFilters(ExceptionContext exceptionContext)
        {
            if (this.GlobalFilters.Count == 0)
            {
                return;
            }

            foreach (IFilter filter in this.GlobalFilters)
            {
                filter.OnException(exceptionContext);
                if (exceptionContext.ExceptionHandled == true) break;
            }

            if (exceptionContext.ExceptionHandled == false)
            {
                throw exceptionContext.Exception;
            }
        }
        /// <summary>
        /// 获取FastApiService实例
        /// </summary>
        /// <param name="actionContext">Api行为上下文</param>
        /// <returns></returns>
        private IFastApiService GetFastApiService(ActionContext actionContext)
        {
            try
            {
                var serviceType = actionContext.Action.DeclaringService;
                var instance = this.DependencyResolver.GetService(serviceType);
                return instance as IFastApiService;
            }
            catch (Exception ex)
            {
                var exception = new ResolveException(actionContext.Action.DeclaringService, ex);
                var exceptionContext = new ExceptionContext(actionContext, exception);

                Common.SendRemoteException(actionContext.Session.UnWrap(), exceptionContext);
                this.ExecGlobalExceptionFilters(exceptionContext);
                return null;
            }
        }
        /// <summary>
        /// 获取Api行为
        /// </summary>
        /// <param name="requestContext">请求上下文</param>
        /// <returns></returns>
        private ApiAction GetApiAction(RequestContext requestContext)
        {
            var action = this.apiActionList.TryGet(requestContext.Packet.ApiName);
            if (action == null)
            {
                var exception = new ApiNotExistException(requestContext.Packet.ApiName);
                var exceptionContext = new ExceptionContext(requestContext, exception);

                Common.SendRemoteException(requestContext.Session.UnWrap(), exceptionContext);
                this.ExecGlobalExceptionFilters(exceptionContext);
            }
            return action;
        }
 /// <summary>
 /// 在Api执行中异常时触发
 /// </summary>
 /// <param name="filterContext">上下文</param>
 protected virtual void OnException(ExceptionContext filterContext)
 {
 }
        /// <summary>
        /// 处理Api行为执行过程中产生的异常
        /// </summary>
        /// <param name="actionContext">上下文</param>
        /// <param name="actionfilters">过滤器</param>
        /// <param name="exception">异常项</param>
        private void ProcessExecutingException(ActionContext actionContext, IEnumerable<IFilter> actionfilters, Exception exception)
        {
            var exceptionContext = new ExceptionContext(actionContext, new ApiExecuteException(actionContext, exception));
            FastTcpCommon.SetRemoteException(actionContext.Session, exceptionContext);
            this.ExecExceptionFilters(actionfilters, exceptionContext);

            if (exceptionContext.ExceptionHandled == false)
            {
                throw exception;
            }
        }
        /// <summary>
        /// 执行异常过滤器
        /// </summary>       
        /// <param name="actionFilters">Api行为过滤器</param>
        /// <param name="exceptionContext">上下文</param>       
        private void ExecExceptionFilters(IEnumerable<IFilter> actionFilters, ExceptionContext exceptionContext)
        {
            foreach (var filter in this.CurrentContext.Session.GlobalFilter.ExceptionFilters)
            {
                if (exceptionContext.ExceptionHandled == false)
                {
                    filter.OnException(exceptionContext);
                }
            }

            if (exceptionContext.ExceptionHandled == false)
            {
                ((IExceptionFilter)this).OnException(exceptionContext);
            }

            foreach (var filter in actionFilters)
            {
                var exceptionFilter = filter as IExceptionFilter;
                if (exceptionFilter != null && exceptionContext.ExceptionHandled == false)
                {
                    exceptionFilter.OnException(exceptionContext);
                }
            }
        }
 /// <summary>
 /// 异常触发
 /// </summary>
 /// <param name="filterContext">上下文</param>  
 void IExceptionFilter.OnException(ExceptionContext filterContext)
 {
     this.OnException(filterContext);
 }
 public void OnException(ExceptionContext filterContext)
 {
     filterContext.ExceptionHandled = true;
 }