public async ValueTask ExecuteAsync(IAppSession session, TPackageInfo package) { if (Filters.Count > 0) { await ExecuteAsyncWithFilter(session, package); return; } var appSession = (TAppSession)session; var asyncCommand = AsyncCommand; if (asyncCommand != null) { await asyncCommand.ExecuteAsync(appSession, package); return; } Command.Execute(appSession, package); }
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); } } } }