public virtual void OnExecuted(ApiCallContext context, FilterArgs args) { if (_overridedExecuted == false && _overridedExecutedAsync) { OnExecutedAsync(context, args).Wait(); } }
/// <summary> /// 触发指定事件 /// </summary> /// <param name="event"></param> /// <param name="context"></param> internal void Invoke(GlobalEvents @event, ApiCallContext context) { var i = (int)@event; if (i < 0 || i >= _events.Length) { return; } if (@event == GlobalEvents.OnUnprocessException && context != null && context.IsError && context.Exception is UnprocessException) { return; //防止递归触发事件 } try { _events[i]?.Invoke(new ApiEventArgs(context)); } catch (Exception ex) { var msg = @event + "事件异常"; context.Error(ex, msg); var newEx = @event == GlobalEvents.OnUnprocessException ? (Exception) new UnprocessException(msg, ex) : new TargetInvocationException(msg, ex); throw newEx; } }
public virtual Task OnExecutedAsync(ApiCallContext context, FilterArgs args) { if (_overridedExecuted && _overridedExecutedAsync == false) { OnExecuted(context, args); } return(null); }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="service"></param> /// <param name="parameters"></param> public ApiExecutingContext( ApiCallContext context, object service, object[] parameters) : base(context.ConnectionId, context.ApiMetadata, context.User) { this.Service = service; this.Parameters = parameters; }
/// <summary> /// 验证对象属性的值 /// </summary> /// <param name="instance"> 待验证属性的对象 </param> /// <param name="context"> api上下文 </param> /// <param name="lazy"> 懒惰模式,存在第一个不合法的参数立即返回,为false则返回所有不合法的验证结果 </param> /// <returns></returns> public static Exception IsValid(object instance, ApiCallContext context, bool lazy) { if (instance == null) { return(null); } List <Exception> errors = null; var props = TypeCache[instance.GetType()]; for (var i = 0; i < props.Count; i++) { var p = props[i]; var value = p.Getter(instance); for (var j = 0; j < p.Count; j++) { var valid = p[j]; if (valid.IsValid(value, context)) { continue; } if (lazy) { return(valid.GetException($"{p.Member.DeclaringType}.{p.Member.Name}", value, context?.Parameters)); } if (errors == null) { errors = new List <Exception>(); } errors.Add(valid.GetException($"{p.Member.DeclaringType}.{p.Member.Name}", value, context?.Parameters)); } var ex = IsValid(value, context, lazy); if (ex == null) { continue; } if (lazy) { return(ex); } if (errors == null) { errors = new List <Exception>(); } errors.AddRange((ex as AggregateException)?.InnerExceptions); } return(errors == null ? null : new AggregateException(errors)); }
public static void Modifies(object instance, ApiCallContext context) { if (instance == null) { return; } var props = TypeCache[instance.GetType()]; for (var i = 0; i < props.Count; i++) { var p = props[i]; var value = p.Getter(instance); for (var j = 0; j < p.Count; j++) { p[j].Modifies(ref value, context); } Modifies(value, context); } }
public async Task RunRpc(ApiCallContext <RequestContext <IScenePeer> > ctx, Func <ApiCallContext <RequestContext <IScenePeer> >, Task> next) { if (_config.EnableApiInstrumentation) { var start = _watch.ElapsedMilliseconds; await next(ctx); _analytics.Push("api", "rpc.s2s", JObject.FromObject(new { type = "RPC", scope = "S2S", inputSize = ctx.Context.InputStream.Length, route = ctx.Route, duration = _watch.ElapsedMilliseconds - start, SessionId = ctx.Context.RemotePeer.SceneId })); } else { await next(ctx); } }
public async Task RunFF(ApiCallContext <Packet <IScenePeerClient> > ctx, Func <ApiCallContext <Packet <IScenePeerClient> >, Task> next) { if (_config.EnableApiInstrumentation) { var start = _watch.ElapsedMilliseconds; await next(ctx); _analytics.Push("api", "ff.cs", JObject.FromObject(new { type = "FireForget", scope = "ClientServer", inputSize = ctx.Context.Stream.Length, route = ctx.Route, duration = _watch.ElapsedMilliseconds - start, SessionId = ctx.Context.Connection.SessionId })); } else { await next(ctx); } }
/// <summary> /// 验证对象值是否有效 /// </summary> /// <param name="value">要验证的值</param> /// <param name="context"> API调用上下文 </param> /// <returns></returns> public virtual bool IsValid(object value, ApiCallContext context) => IsValid(value, context?.Parameters);
/// <summary> /// 获取异常信息 /// </summary> /// <param name="name">参数名</param> /// <param name="value">验证失败的值</param> /// <param name="context"> API调用上下文 </param> /// <returns></returns> public virtual Exception GetException(string name, object value, ApiCallContext context) => GetException(name, value, context?.Parameters);
public ApiEventArgs(ApiCallContext context) { Context = context; }
public FilterArgs(ApiCallContext context, IResultUpdater resultUpdater) { Context = context; _resultUpdater = resultUpdater; }
/// <summary> /// 将一个值更改为类型相同的新值 /// </summary> /// <param name="arg">待修改的值</param> /// <param name="context"> API调用上下文 </param> /// <returns></returns> public virtual void Modifies(ref object arg, ApiCallContext context) => Modifies(ref arg);