public ValueTask OnEngineCompleteAsync(ConsoleAppContext context, string?errorMessageIfFailed, Exception?exceptionIfExists) { this.CompleteSuccessfully = (errorMessageIfFailed == null && exceptionIfExists == null); this.ErrorMessage = errorMessageIfFailed; this.Exception = exceptionIfExists; return(innerInterceptor.OnEngineCompleteAsync(context, errorMessageIfFailed, exceptionIfExists)); }
public async Task StopAsync(CancellationToken cancellationToken) { try { cancellationTokenSource?.Cancel(); var task = runningTask; if (task != null) { logger.LogTrace("Detect Cancel signal, wait for running console app task canceled."); await task; logger.LogTrace("ConsoleApp cancel completed."); } } finally { await interceptor.OnEngineCompleteAsync(this.scope.ServiceProvider, logger); scope.Dispose(); } }
public ValueTask OnEngineCompleteAsync(IServiceProvider serviceProvider, ILogger <ConsoleAppEngine> logger) { return(innerInterceptor.OnEngineCompleteAsync(serviceProvider, logger)); }
async Task RunCore(ConsoleAppContext ctx, Type type, MethodInfo methodInfo, string?[] args, int argsOffset) { object instance; object[] invokeArgs; try { if (!TryGetInvokeArguments(methodInfo.GetParameters(), args, argsOffset, out invokeArgs, out var errorMessage)) { await SetFailAsync(ctx, errorMessage + " args: " + string.Join(" ", args)); return; } } catch (Exception ex) { await SetFailAsync(ctx, "Fail to match method parameter on " + type.Name + "." + methodInfo.Name + ". args: " + string.Join(" ", args), ex); return; } try { instance = provider.GetService(type); typeof(ConsoleAppBase).GetProperty(nameof(ConsoleAppBase.Context)).SetValue(instance, ctx); } catch (Exception ex) { await SetFailAsync(ctx, "Fail to create ConsoleAppBase instance. Type:" + type.FullName, ex); return; } try { var result = methodInfo.Invoke(instance, invokeArgs); switch (result) { case int exitCode: Environment.ExitCode = exitCode; break; case Task <int> taskWithExitCode: Environment.ExitCode = await taskWithExitCode; break; case Task task: await task; break; } } catch (Exception ex) { if (ex is OperationCanceledException operationCanceledException && operationCanceledException.CancellationToken == cancellationToken) { // NOTE: Do nothing if the exception has thrown by the CancellationToken of ConsoleAppEngine. // If the user code throws OperationCanceledException, ConsoleAppEngine should not handle that. return; } if (ex is TargetInvocationException tex) { await SetFailAsync(ctx, "Fail in console app running on " + type.Name + "." + methodInfo.Name, tex.InnerException); return; } else { await SetFailAsync(ctx, "Fail in console app running on " + type.Name + "." + methodInfo.Name, ex); return; } } await interceptor.OnEngineCompleteAsync(ctx, null, null); logger.LogTrace("ConsoleAppEngine.Run Complete Successfully"); }