/// <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(exception)); this.Server.SendRemoteException(exceptionContext); this.ExecAllExceptionFilters(actionfilters, exceptionContext); }
/// <summary> /// 调用自身实现的Api行为 /// 将返回值发送给客户端 /// </summary> /// <param name="actionContext">上下文</param> /// <param name="filters">过滤器</param> /// <exception cref="ArgumentException"></exception> /// <returns>当正常执行输出Api的结果时返回true</returns> private bool ExecuteAction(ActionContext actionContext, IEnumerable<IFilter> filters) { var packet = actionContext.Packet; var action = actionContext.Action; var session = actionContext.Session; var serializer = this.Server.JsonSerializer; var parameters = this.GetAndUpdateParameterValues(actionContext); // Api执行前 this.ExecFiltersBeforeAction(filters, actionContext); if (actionContext.Result != null) { var exceptionContext = new ExceptionContext(actionContext, actionContext.Result); this.Server.SendRemoteException(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); this.Server.SendRemoteException(exceptionContext); return false; } // 返回数据 if (action.IsVoidReturn == false && session.IsConnected) { packet.body = apiResult; var packetJson = serializer.Serialize(packet); session.UnWrap().SendText(packetJson); } return true; }
/// <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="serializer">序列化工具</param> /// <param name="exceptionContext">上下文</param> /// <returns></returns> public static bool SetRemoteException(IDynamicJsonSerializer serializer, ExceptionContext exceptionContext) { try { var packet = exceptionContext.Packet; packet.state = false; packet.body = exceptionContext.Exception.Message; var packetJson = serializer.Serialize(packet); exceptionContext.Session.SendText(packetJson); return true; } catch (Exception) { return false; } }
/// <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(exception)); Common.SetRemoteException(this.Server.JsonSerializer, exceptionContext); this.ExecExceptionFilters(actionfilters, exceptionContext); if (exceptionContext.ExceptionHandled == false) { throw exception; } }
protected override void OnException(ExceptionContext filterContext) { Console.WriteLine(filterContext.Exception.Message); filterContext.ExceptionHandled = true; }