コード例 #1
0
        public async Task <IResult> ExecuteAsync(ITurnContext 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)
                    {
                        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));
            }
        }
コード例 #2
0
        private async Task <IResult> ExecuteInternalAsync(ITurnContext 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);

                    await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false);

                    if (result is RuntimeResult execResult)
                    {
                        return(execResult);
                    }
                }
                else if (task is Task <ExecuteResult> execTask)
                {
                    var result = await execTask.ConfigureAwait(false);

                    await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false);

                    return(result);
                }
                else
                {
                    await task.ConfigureAwait(false);

                    var result = ExecuteResult.FromSuccess();
                    await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false);
                }

                var executeResult = ExecuteResult.FromSuccess();
                return(executeResult);
            }
            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);

                var result = ExecuteResult.FromError(ex);

                if (Module.Service._throwOnError)
                {
                    if (ex == originalEx)
                    {
                        throw;
                    }
                    else
                    {
                        ExceptionDispatchInfo.Capture(ex).Throw();
                    }
                }

                return(result);
            }
            finally
            {
                //TODO add login back --
                //await Module.Service._cmdLogger.VerboseAsync($"Executed {GetLogText(context)}").ConfigureAwait(false);
            }
        }