Exemplo n.º 1
0
 public BatchEngine(ILogger <BatchEngine> logger, IServiceProvider provider, IBatchInterceptor interceptor, CancellationToken cancellationToken)
 {
     this.logger            = logger;
     this.provider          = provider;
     this.interceptor       = interceptor;
     this.cancellationToken = cancellationToken;
 }
Exemplo n.º 2
0
 public BatchEngineMiddleware(RequestDelegate next, ILogger <BatchEngine> logger, IBatchInterceptor interceptor, IServiceProvider provider, TargetBatchTypeCollection targetTypes)
 {
     this.next         = next;
     this.logger       = logger;
     this.interceptor  = interceptor;
     this.provider     = provider;
     this.methodLookup = BuildMethodLookup(targetTypes);
 }
 public BatchEngineService(IApplicationLifetime appLifetime, Type type, MethodInfo methodInfo, string[] args, ILogger <BatchEngine> logger, IServiceProvider provider)
 {
     this.args                    = args;
     this.type                    = type;
     this.methodInfo              = methodInfo;
     this.appLifetime             = appLifetime;
     this.provider                = provider;
     this.logger                  = logger;
     this.interceptor             = (provider.GetService(typeof(IBatchInterceptor)) as IBatchInterceptor) ?? NullBatchInerceptor.Default;
     this.cancellationTokenSource = new CancellationTokenSource();
 }
Exemplo n.º 4
0
        public static IHostBuilder UseBatchEngine <T>(this IHostBuilder hostBuilder, string[] args, IBatchInterceptor interceptor = null, bool useSimpleConosoleLogger = true)
            where T : BatchBase
        {
            if (args.Length == 0)
            {
                var method        = typeof(T).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                var defaultMethod = method.FirstOrDefault(x => x.GetCustomAttribute <CommandAttribute>() == null);
                if (defaultMethod == null || defaultMethod.GetParameters().Length != 0)
                {
                    Console.WriteLine(BuildHelpParameter(method));
                    hostBuilder.ConfigureServices(services =>
                    {
                        services.AddOptions <ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
                        services.AddSingleton <IHostedService, EmptyHostedService>();
                    });
                    return(hostBuilder);
                }
            }

            if (args.Length == 1 && args[0].Equals(ListCommand, StringComparison.OrdinalIgnoreCase))
            {
                ShowMethodList();
                hostBuilder.ConfigureServices(services =>
                {
                    services.AddOptions <ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
                    services.AddSingleton <IHostedService, EmptyHostedService>();
                });
                return(hostBuilder);
            }
            if (args.Length == 1 && args[0].Equals(HelpCommand, StringComparison.OrdinalIgnoreCase))
            {
                var method = typeof(T).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                Console.WriteLine(BuildHelpParameter(method));
                hostBuilder.ConfigureServices(services =>
                {
                    services.AddOptions <ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
                    services.AddSingleton <IHostedService, EmptyHostedService>();
                });
                return(hostBuilder);
            }

            hostBuilder = hostBuilder.ConfigureServices(services =>
            {
                services.AddOptions <ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
                services.AddSingleton <string[]>(args);
                services.AddSingleton <Type>(typeof(T));
                services.AddSingleton <IHostedService, BatchEngineService>();
                services.AddSingleton <IBatchInterceptor>(interceptor ?? NullBatchInerceptor.Default);
                services.AddTransient <T>();
            });

            if (useSimpleConosoleLogger)
            {
                hostBuilder = hostBuilder.ConfigureLogging(x => x.AddSimpleConsole());
            }

            return(hostBuilder.UseConsoleLifetime());
        }
Exemplo n.º 5
0
 public static Task RunBatchEngineAsync(this IHostBuilder hostBuilder, string[] args, IBatchInterceptor interceptor = null, bool useSimpleConosoleLogger = true)
 {
     return(UseBatchEngine(hostBuilder, args, interceptor, useSimpleConosoleLogger).Build().RunAsync());
 }
Exemplo n.º 6
0
        public static IHostBuilder UseBatchEngine(this IHostBuilder hostBuilder, string[] args, IBatchInterceptor interceptor = null, bool useSimpleConosoleLogger = true)
        {
            if (args.Length == 0 || (args.Length == 1 && args[0].Equals(ListCommand, StringComparison.OrdinalIgnoreCase)))
            {
                ShowMethodList();
                hostBuilder.ConfigureServices(services =>
                {
                    services.AddOptions <ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
                    services.AddSingleton <IHostedService, EmptyHostedService>();
                });
                return(hostBuilder);
            }
            if (args.Length == 2 && args[0].Equals(HelpCommand, StringComparison.OrdinalIgnoreCase))
            {
                var(t, mi) = GetTypeFromAssemblies(args[1]);
                if (mi != null)
                {
                    Console.WriteLine(BuildHelpParameter(new[] { mi }));
                }
                else
                {
                    Console.WriteLine("Method not found , please check \"list\" command.");
                }
                hostBuilder.ConfigureServices(services =>
                {
                    services.AddOptions <ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
                    services.AddSingleton <IHostedService, EmptyHostedService>();
                });
                return(hostBuilder);
            }

            Type       type       = null;
            MethodInfo methodInfo = null;

            if (args.Length >= 1)
            {
                (type, methodInfo) = GetTypeFromAssemblies(args[0]);
            }

            hostBuilder = hostBuilder
                          .ConfigureServices(services =>
            {
                services.AddOptions <ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
                services.AddSingleton <string[]>(args);
                services.AddSingleton <IHostedService, BatchEngineService>();
                services.AddSingleton <IBatchInterceptor>(interceptor ?? NullBatchInerceptor.Default);
                if (type != null)
                {
                    services.AddSingleton <Type>(type);
                    services.AddTransient(type);
                }
                else
                {
                    services.AddSingleton <Type>(typeof(void));
                }

                if (methodInfo != null)
                {
                    services.AddSingleton <MethodInfo>(methodInfo);
                }
            });

            if (useSimpleConosoleLogger)
            {
                hostBuilder = hostBuilder.ConfigureLogging(x => x.AddSimpleConsole());
            }

            return(hostBuilder.UseConsoleLifetime());
        }
        public static IHostBuilder UseBatchEngine <T>(this IHostBuilder hostBuilder, string[] args, IBatchInterceptor interceptor = null)
            where T : BatchBase
        {
            var method        = typeof(T).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            var defaultMethod = method.FirstOrDefault(x => x.GetCustomAttribute <CommandAttribute>() == null);
            var hasList       = method.Any(x => x.GetCustomAttribute <CommandAttribute>()?.EqualsAny(ListCommand) ?? false);
            var hasHelp       = method.Any(x => x.GetCustomAttribute <CommandAttribute>()?.EqualsAny(HelpCommand) ?? false);

            if (args.Length == 0)
            {
                if (defaultMethod == null || (defaultMethod.GetParameters().Length != 0 && !defaultMethod.GetParameters().All(x => x.HasDefaultValue)))
                {
                    if (!hasHelp)
                    {
                        Console.WriteLine(BatchEngine.BuildHelpParameter(method));
                        hostBuilder.ConfigureServices(services =>
                        {
                            services.AddOptions <ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
                            services.AddSingleton <IHostedService, EmptyHostedService>();
                        });
                        return(hostBuilder);
                    }
                    else
                    {
                        // override default Help
                        args = new string[] { "help" };
                    }
                }
            }

            if (!hasList && args.Length == 1 && args[0].Equals(ListCommand, StringComparison.OrdinalIgnoreCase))
            {
                ShowMethodList();
                hostBuilder.ConfigureServices(services =>
                {
                    services.AddOptions <ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
                    services.AddSingleton <IHostedService, EmptyHostedService>();
                });
                return(hostBuilder);
            }

            if (!hasHelp && args.Length == 1 && args[0].Equals(HelpCommand, StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine(BatchEngine.BuildHelpParameter(method));
                hostBuilder.ConfigureServices(services =>
                {
                    services.AddOptions <ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
                    services.AddSingleton <IHostedService, EmptyHostedService>();
                });
                return(hostBuilder);
            }

            hostBuilder = hostBuilder.ConfigureServices(services =>
            {
                services.AddOptions <ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
                services.AddSingleton <string[]>(args);
                services.AddSingleton <Type>(typeof(T));
                services.AddSingleton <IHostedService, BatchEngineService>();
                services.AddSingleton <IBatchInterceptor>(interceptor ?? NullBatchInterceptor.Default);
                services.AddTransient <T>();
            });

            return(hostBuilder.UseConsoleLifetime());
        }
 public static Task RunBatchEngineAsync(this IHostBuilder hostBuilder, string[] args, IBatchInterceptor interceptor = null)
 {
     return(UseBatchEngine(hostBuilder, args, interceptor).Build().RunAsync());
 }
Exemplo n.º 9
0
 public WebHostingInterceptor(IBatchInterceptor innerInterceptor)
 {
     this.innerInterceptor = innerInterceptor;
 }
Exemplo n.º 10
0
        public static Task RunBatchEngine <T>(this IHostBuilder hostBuilder, string[] args, IBatchInterceptor interceptor = null)
            where T : BatchBase
        {
            if (args.Length == 1 && args[0].Equals(ListCommand, StringComparison.OrdinalIgnoreCase))
            {
                ShowMethodList();
                return(Task.CompletedTask);
            }
            if (args.Length == 1 && args[0].Equals(HelpCommand, StringComparison.OrdinalIgnoreCase))
            {
                var method = typeof(T).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).First();
                Console.WriteLine(BuildHelpParameter(method));
                return(Task.CompletedTask);
            }

            return(hostBuilder.ConfigureServices(services =>
            {
                services.AddOptions <ConsoleLifetimeOptions>().Configure(x => x.SuppressStatusMessages = true);
                services.AddSingleton <string[]>(args);
                services.AddSingleton <Type>(typeof(T));
                services.AddSingleton <IHostedService, BatchEngineService>();
                services.AddSingleton <IBatchInterceptor>(interceptor ?? NullBatchInerceptor.Default);
                services.AddTransient <T>();
            })
                   .RunConsoleAsync());
        }
Exemplo n.º 11
0
 public static Task RunBatchEngineWebHosting(this IWebHostBuilder builder, string urls, SwaggerOptions swaggerOptions = null, IBatchInterceptor interceptor = null)
 {
     return(builder
            .PrepareBatchEngineMiddleware(interceptor)
            .ConfigureServices(services =>
     {
         if (swaggerOptions == null)
         {
             var entryAsm = Assembly.GetEntryAssembly();
             var xmlName = entryAsm.GetName().Name + ".xml";
             var xmlPath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), xmlName);
             swaggerOptions = new SwaggerOptions(entryAsm.GetName().Name, "", "/")
             {
                 XmlDocumentPath = xmlPath
             };
         }
         services.AddSingleton <SwaggerOptions>(swaggerOptions);
     })
            .UseKestrel()
            .UseUrls(urls)
            .UseStartup <DefaultStartup>()
            .Build()
            .RunAsync());
 }
Exemplo n.º 12
0
        public static IWebHostBuilder PrepareBatchEngineMiddleware(this IWebHostBuilder builder, IBatchInterceptor interceptor = null)
        {
            var batchTypes = CollectBatchTypes();
            var target     = new TargetBatchTypeCollection(batchTypes);

            return(builder
                   .ConfigureServices(services =>
            {
                services.AddSingleton <IBatchInterceptor>(interceptor ?? NullBatchInerceptor.Default);
                services.AddSingleton <TargetBatchTypeCollection>(target);
                foreach (var item in target)
                {
                    services.AddTransient(item);
                }
            }));
        }