public ValueTask OnMethodEndAsync(ConsoleAppContext context, string?errorMessageIfFailed, Exception?exceptionIfExists)
 {
     this.CompleteSuccessfully = (errorMessageIfFailed == null && exceptionIfExists == null);
     this.ErrorMessage         = errorMessageIfFailed;
     this.Exception            = exceptionIfExists;
     return(innerInterceptor.OnMethodEndAsync(context, errorMessageIfFailed, exceptionIfExists));
 }
Exemple #2
0
        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.OnMethodEndAsync();

                scope.Dispose();
            }
        }
Exemple #3
0
 public ValueTask OnMethodEndAsync()
 {
     return(innerInterceptor.OnMethodEndAsync());
 }
        public async Task RunAsync(Type type, string[] args)
        {
            logger.LogTrace("ConsoleAppEngine.Run Start");

            int        argsOffset = 0;
            MethodInfo?method     = null;
            var        ctx        = new ConsoleAppContext(args, DateTime.UtcNow, cancellationToken, logger);

            try
            {
                await interceptor.OnMethodBeginAsync(ctx);

                if (type == typeof(void))
                {
                    await SetFailAsync(ctx, "Type or method does not found on this Program. args: " + string.Join(" ", args));

                    return;
                }

                var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                if (methods.Length == 0)
                {
                    await SetFailAsync(ctx, "Method can not select. T of Run/UseConsoleAppEngine<T> have to be contain single method or command. Type:" + type.FullName);

                    return;
                }

                MethodInfo?helpMethod = null;
                foreach (var item in methods)
                {
                    var command = item.GetCustomAttribute <CommandAttribute>();
                    if (command != null)
                    {
                        if (args.Length > 0 && command.EqualsAny(args[0]))
                        {
                            // command's priority is first
                            method     = item;
                            argsOffset = 1;
                            goto RUN;
                        }
                        else
                        {
                            if (command.EqualsAny("help"))
                            {
                                helpMethod = item;
                            }
                        }
                    }
                    else
                    {
                        if (method != null)
                        {
                            await SetFailAsync(ctx, "Found more than one public methods(without command). Type:" + type.FullName + " Method:" + method.Name + " and " + item.Name);

                            return;
                        }
                        method = item; // found single public(non-command) method.
                    }
                }

                if (method != null)
                {
                    goto RUN;
                }

                // completely not found, invalid command name show help.
                if (helpMethod != null)
                {
                    method = helpMethod;
                    goto RUN;
                }
                else
                {
                    Console.Write(new CommandHelpBuilder().BuildHelpMessage(methods, null));
                    await interceptor.OnMethodEndAsync(ctx, null, null);

                    return;
                }
            }
            catch (Exception ex)
            {
                await SetFailAsync(ctx, "Fail to get method. Type:" + type.FullName, ex);

                return;
            }

RUN:
            await RunCore(ctx, type, method, args, argsOffset);
        }