/// <summary> /// 获取和更新Api行为的参数值 /// </summary> /// <param name="context">上下文</param> /// <exception cref="ArgumentException"></exception> /// <returns></returns> public static object[] GetAndUpdateParameterValues(ActionContext context) { var body = context.Packet.body as IList; if (body == null) { throw new ArgumentException("body参数必须为数组"); } if (body.Count != context.Action.ParameterTypes.Length) { throw new ArgumentException("body参数数量不正确"); } var parameters = new object[body.Count]; var serializer = context.Session.Server.JsonSerializer; for (var i = 0; i < body.Count; i++) { var bodyParameter = body[i]; var parameterType = context.Action.ParameterTypes[i]; parameters[i] = serializer.Convert(bodyParameter, parameterType); } context.Action.ParameterValues = parameters; return parameters; }
/// <summary> /// 执行Api行为 /// </summary> /// <param name="actionContext">上下文</param> void IJsonWebSocketApiService.Execute(ActionContext actionContext) { var filters = Enumerable.Empty<IFilter>(); try { this.CurrentContext = actionContext; filters = this.Server.FilterAttributeProvider.GetActionFilters(actionContext.Action); this.ExecuteAction(actionContext, filters); } catch (AggregateException exception) { foreach (var inner in exception.InnerExceptions) { this.ProcessExecutingException(actionContext, filters, inner); } } catch (Exception exception) { this.ProcessExecutingException(actionContext, filters, exception); } finally { this.CurrentContext = null; } }
protected override void OnExecuting(ActionContext filterContext) { if (filterContext.Session.Tag.TryGet("name") == null) { filterContext.Result = "请设置昵称后再聊天"; } }
/// <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="filters">Api行为过滤器</param> /// <param name="actionContext">上下文</param> private void ExecFiltersAfterAction(IEnumerable<IFilter> filters, ActionContext actionContext) { var totalFilters = this.GetTotalFilters(filters); foreach (var filter in totalFilters) { filter.OnExecuted(actionContext); if (actionContext.Result != null) break; } }
/// <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="context">请求上下文</param> /// <param name="exception">异常</param> public ExceptionContext(ActionContext context, Exception exception) : base(context.Session, context.Packet, context.AllSessions) { this.Exception = exception; }
/// <summary> /// 在执行Api行为后触发 /// </summary> /// <param name="filterContext">上下文</param> protected virtual void OnExecuted(ActionContext 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; } }