private async ValueTask ExecuteAsyncWithFilter(IAppSession session, TPackageInfo package) { var context = new CommandExecutingContext(); context.Package = package; context.Session = session; var command = AsyncCommand != null ? (AsyncCommand as ICommand) : (Command as ICommand); if (command is ICommandWrap commandWrap) { command = commandWrap.InnerCommand; } context.CurrentCommand = command; var filters = Filters; var continued = true; for (var i = 0; i < filters.Count; i++) { var f = filters[i]; if (f is AsyncCommandFilterAttribute asyncCommandFilter) { continued = await asyncCommandFilter.OnCommandExecutingAsync(context); } else if (f is CommandFilterAttribute commandFilter) { continued = commandFilter.OnCommandExecuting(context); } if (!continued) { break; } } if (!continued) { return; } try { var appSession = (TAppSession)session; var asyncCommand = AsyncCommand; if (asyncCommand != null) { await asyncCommand.ExecuteAsync(appSession, package); } else { Command.Execute(appSession, package); } } catch (Exception e) { context.Exception = e; } finally { for (var i = 0; i < filters.Count; i++) { var f = filters[i]; if (f is AsyncCommandFilterAttribute asyncCommandFilter) { await asyncCommandFilter.OnCommandExecutedAsync(context); } else if (f is CommandFilterAttribute commandFilter) { commandFilter.OnCommandExecuted(context); } } } }
/// <summary> /// Called when [command executed]. /// 命令执行后 /// </summary> /// <param name="commandContext">The command context.命令上下文</param> public abstract ValueTask OnCommandExecutedAsync(CommandExecutingContext commandContext);
/// <summary> /// Called when [command executed]. /// 命令执行后 /// </summary> /// <param name="commandContext">The command context.</param> public abstract void OnCommandExecuted(CommandExecutingContext commandContext);
/// <summary> /// Called when [command executing]. /// 命令执行时 /// </summary> /// <param name="commandContext">命令上下文</param> /// <returns>return if the service should continue to process this session.如果服务继续处理此Seesion时返回</returns> public abstract ValueTask <bool> OnCommandExecutingAsync(CommandExecutingContext commandContext);
/// <summary> /// Called when [command executing]. /// 命令执行时 /// </summary> /// <param name="commandContext">The command context.</param> public abstract bool OnCommandExecuting(CommandExecutingContext commandContext);