public async Task <int> Execute(CommandArgs args) { int result = -1; bool visualized = false; using (ITransaction transaction = transactionFactory.StartTransaction(out ChangeObservable observable)) using (ExecutionContext.RegisterObservable(observable)) { try { CommandResult commandResult = executeAsync ? await ExecuteDetailedAsync((T)args, observable).ConfigureAwait(false) : await Task.Run(() => ExecuteDetailed((T)args, observable)).ConfigureAwait(false); result = commandResult.ExternalResult; if (commandResult.DetailedResult != null) { commandResultVisualizer.Visualize(commandResult.DetailedResult, args); visualized = true; } commandResult.Exceptions.ThrowIfNotEmpty(); transaction.OnCompleted(); } catch (Exception e) { if (!exceptionHandler.HandleException(e)) { throw; } result = -1; } finally { if (!visualized && args.Deprecated) { ExecutionContext.WriteWarning($"This command is deprecated. Please use '{args.DeprecatedAlternative}' instead."); } } } return(result); }
public async Task <int> Execute(CommandArgs args) { int result = -1; bool visualized = false; using (ITransaction transaction = transactionFactory.StartTransaction(out ChangeObservable observable)) using (ExecutionContext.RegisterObservable(observable)) { IDisposable redirectedToken = null; try { StringBuilderUserInterface stringBuilderUserInterface = new StringBuilderUserInterface( ExecutionContext, true, true, true, true); if (hasDetailedResult) { redirectedToken = ExecutionContext.RedirectOutput(stringBuilderUserInterface); } CommandResult commandResult = null; try { commandResult = executeAsync ? await ExecuteDetailedAsync((T)args, observable).ConfigureAwait(false) : await Task.Run(() => ExecuteDetailed((T)args, observable)).ConfigureAwait(false); result = commandResult.ExternalResult; } catch (Exception e) { //Include exceptions in error output to include them in the json output later when detailed results are available if (!exceptionHandler.HandleException(e)) { throw; } result = -1; } if (redirectedToken != null) { redirectedToken.Dispose(); commandResultVisualizer.Visualize(commandResult?.DetailedResult, args, stringBuilderUserInterface.Error); visualized = true; } commandResult?.Exceptions.ThrowIfNotEmpty(); if (result != -1) { transaction.OnCompleted(); } } catch (Exception e) { if (!exceptionHandler.HandleException(e)) { throw; } result = -1; } finally { redirectedToken?.Dispose(); if (!visualized && args.Deprecated) { ExecutionContext.WriteWarning($"This command is deprecated. Please use '{args.DeprecatedAlternative}' instead."); } } } return(result); }