Esempio n. 1
0
        private async Task ExecuteAsyncInternal(ICommandContext context, object[] args, IDependencyMap map)
        {
            await Module.Service._cmdLogger.DebugAsync($"Executing {GetLogText(context)}").ConfigureAwait(false);

            try
            {
                await _action(context, args, map).ConfigureAwait(false);
            }
            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();
                    }
                }
            }
            await Module.Service._cmdLogger.VerboseAsync($"Executed {GetLogText(context)}").ConfigureAwait(false);
        }
Esempio n. 2
0
        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);
            }
        }