private async Task <IResult> ExecuteAsyncInternal(ICommandContext context, object[] args, IServiceProvider services) { await Module.Service._cmdLogger.DebugAsync($"Executing {GetLogText(context)}").ConfigureAwait(false); try { var task = _action(context, args, services, this); if (task is Task <IResult> resultTask) { var result = await resultTask.ConfigureAwait(false); if (result is RuntimeResult execResult) { return(execResult); } } else if (task is Task <ExecuteResult> execTask) { return(await execTask.ConfigureAwait(false)); } else { await task.ConfigureAwait(false); } return(ExecuteResult.FromSuccess()); } catch (Exception ex) { var originalEx = ex; while (ex is TargetInvocationException) //Happens with void-returning commands { ex = ex.InnerException; } var wrappedEx = new CommandException(this, context, ex); await Module.Service._cmdLogger.ErrorAsync(wrappedEx).ConfigureAwait(false); if (Module.Service._throwOnError) { if (ex == originalEx) { throw; } else { ExceptionDispatchInfo.Capture(ex).Throw(); } } return(ExecuteResult.FromError(CommandError.Exception, ex.Message)); } finally { await Module.Service._cmdLogger.VerboseAsync($"Executed {GetLogText(context)}").ConfigureAwait(false); } }
public async Task <ExecuteResult> Execute(IUserMessage context, IEnumerable <object> argList, IEnumerable <object> paramList) { try { await _action.Invoke(context, GenerateArgs(argList, paramList)).ConfigureAwait(false);//Note: This code may need context return(ExecuteResult.FromSuccess()); } catch (Exception ex) { return(ExecuteResult.FromError(ex)); } }
public async Task <ExecuteResult> ExecuteAsync(ICommandContext context, IEnumerable <object> argList, IEnumerable <object> paramList, IDependencyMap map) { if (map == null) { map = DependencyMap.Empty; } try { object[] args = GenerateArgs(argList, paramList); for (int position = 0; position < Parameters.Count; position++) { var parameter = Parameters[position]; var argument = args[position]; var result = await parameter.CheckPreconditionsAsync(context, argument, map).ConfigureAwait(false); if (!result.IsSuccess) { return(ExecuteResult.FromError(result)); } } switch (RunMode) { case RunMode.Sync: //Always sync await ExecuteAsyncInternal(context, args, map).ConfigureAwait(false); break; case RunMode.Async: //Always async var t2 = Task.Run(async() => { await ExecuteAsyncInternal(context, args, map).ConfigureAwait(false); }); break; } return(ExecuteResult.FromSuccess()); } catch (Exception ex) { return(ExecuteResult.FromError(ex)); } }
public async Task <ExecuteResult> ExecuteAsync(ICommandContext context, IEnumerable <object> argList, IEnumerable <object> paramList, IDependencyMap map) { if (map == null) { map = DependencyMap.Empty; } try { object[] args = GenerateArgs(argList, paramList); foreach (var parameter in Parameters) { var result = await parameter.CheckPreconditionsAsync(context, args, map).ConfigureAwait(false); if (!result.IsSuccess) { return(ExecuteResult.FromError(result)); } } switch (RunMode) { case RunMode.Sync: //Always sync await _action(context, args, map).ConfigureAwait(false); break; case RunMode.Mixed: //Sync until first await statement var t1 = _action(context, args, map); break; case RunMode.Async: //Always async var t2 = Task.Run(() => _action(context, args, map)); break; } return(ExecuteResult.FromSuccess()); } catch (Exception ex) { return(ExecuteResult.FromError(ex)); } }
public async Task <IResult> ExecuteAsync(ICommandContext context, IEnumerable <object> argList, IEnumerable <object> paramList, IServiceProvider services) { services = services ?? EmptyServiceProvider.Instance; try { object[] args = GenerateArgs(argList, paramList); for (int position = 0; position < Parameters.Count; position++) { var parameter = Parameters[position]; object argument = args[position]; var result = await parameter.CheckPreconditionsAsync(context, argument, services).ConfigureAwait(false); if (!result.IsSuccess) { await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false); return(ExecuteResult.FromError(result)); } } switch (RunMode) { case RunMode.Sync: //Always sync return(await ExecuteInternalAsync(context, args, services).ConfigureAwait(false)); case RunMode.Async: //Always async var t2 = Task.Run(async() => { await ExecuteInternalAsync(context, args, services).ConfigureAwait(false); }); break; } return(ExecuteResult.FromSuccess()); } catch (Exception ex) { return(ExecuteResult.FromError(ex)); } }
private static void BuildCommand(CommandBuilder builder, TypeInfo typeInfo, MethodInfo method, CommandService service, IServiceProvider serviceprovider) { var attributes = method.GetCustomAttributes(); foreach (var attribute in attributes) { switch (attribute) { case CommandAttribute command: builder.AddAliases(command.Text); builder.RunMode = command.RunMode; builder.Name = builder.Name ?? command.Text; break; case NameAttribute name: builder.Name = name.Text; break; case PriorityAttribute priority: builder.Priority = priority.Priority; break; case SummaryAttribute summary: builder.Summary = summary.Text; break; case RemarksAttribute remarks: builder.Remarks = remarks.Text; break; case AliasAttribute alias: builder.AddAliases(alias.Aliases); break; case PreconditionAttribute precondition: builder.AddPrecondition(precondition); break; default: builder.AddAttributes(attribute); break; } } if (builder.Name == null) { builder.Name = method.Name; } var parameters = method.GetParameters(); int pos = 0, count = parameters.Length; foreach (var paramInfo in parameters) { builder.AddParameter((parameter) => { BuildParameter(parameter, paramInfo, pos++, count, service, serviceprovider); }); } var createInstance = ReflectionUtils.CreateBuilder <IModuleBase>(typeInfo, service); async Task <IResult> ExecuteCallback(ICommandContext context, object[] args, IServiceProvider services, CommandInfo cmd) { var instance = createInstance(services); instance.SetContext(context); try { instance.BeforeExecute(cmd); var task = method.Invoke(instance, args) as Task ?? Task.Delay(0); if (task is Task <RuntimeResult> resultTask) { return(await resultTask.ConfigureAwait(false)); } else { await task.ConfigureAwait(false); return(ExecuteResult.FromSuccess()); } } finally { instance.AfterExecute(cmd); (instance as IDisposable)?.Dispose(); } } builder.Callback = ExecuteCallback; }