private static async Task Main(string[] args) { var parser = new Parser(settings => { settings.IgnoreUnknownArguments = true; settings.CaseSensitive = false; settings.AutoHelp = true; }); IHost host = new ConsoleHostBuilder(args) .ParseArguments <CommandLineOptions>(parser) .ConfigureServices((context, services) => { services.AddHostedService <ConsoleRunner>(); }) .ConfigureLogging((context, logging) => { logging .ClearProviders() .AddSimpleConsole(); }) // Listen for CTRL+C or SIGTERM. .UseConsoleLifetime() .Build(); // Setup static logger factory. QboxNextLogProvider.LoggerFactory = host.Services.GetRequiredService <ILoggerFactory>(); using (host) { await host.StartWithArgumentsAsync <CommandLineOptions>(); } }
static async Task Main(string[] args) { var serviceName = "TestBot"; var host = ConsoleHostBuilder .Create() .AsService(serviceName) .WithConfiguration(configurationBuilder => { configurationBuilder.AddJsonFile("appsettings.json"); }) .WithLogger(loggerConfiguration => loggerConfiguration.WriteTo.Console()) .WithMessaging(serviceName, typeof(StartCommandHandler).Assembly) .WithRabbitMQMessaging(config => new RabbitMQMessagingSettings( hostName: config["RabbitMQ:HostName"], userName: config["RabbitMQ:UserName"], password: config["RabbitMQ:Password"]) ) .WithTelegramBot <IConsoleHostBuilder, TestBotCommandResolver>(config => new TelegramBotSettings( name: config["botName"], token: config["botToken"], apiURL: config["apiURL"]) ) .Build(); await host.Run(); }
private static async Task Main(string[] args) { // Make sure that the default parsing and formatting of numbers is using '.' as floating point. Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; var parser = new Parser(settings => { settings.IgnoreUnknownArguments = true; settings.CaseSensitive = false; settings.AutoHelp = true; settings.AutoVersion = false; }); IHost host = new ConsoleHostBuilder(args) .ParseArguments <QboxSimulatorOptions>(parser) .ConfigureServices((context, services) => { services.AddHostedService <ConsoleRunner>(); }) .ConfigureLogging((context, logging) => { logging .ClearProviders() .AddNLog(); }) // Listen for CTRL+C or SIGTERM. .UseConsoleLifetime() .Build(); // Setup static logger factory. QboxNextLogProvider.LoggerFactory = host.Services.GetRequiredService <ILoggerFactory>(); await host.RunWithArgumentsAsync <QboxSimulatorOptions>(); }
public static void Main(string[] args) => ConsoleHostBuilder.CreateAndRunApplication <Program>( arguments: args, configureServicesDelegate: (hostingContext, services) => { services.AddAndConfigureApplication(); services.AddAndConfigureSqlServerPersistence(hostingContext.Configuration); });
public void Throws_ConsoleHostBuilderException_when_no_Run_method_with_return_type_Task_is_defined() { var exception = Assert.Throws <ConsoleHostBuilderException>(() => { ConsoleHostBuilder.CreateAndRunApplication <ConsoleHostBuilderExtensionsTests>(); }); exception.Message.Contains(" because the 'Run' method's return type is not of type 'Task'"); }
public ApplicationDbContextFactory() { _host = ConsoleHostBuilder.CreateHost <ApplicationDbContext>(new string[] { }); _logger = GetService <ILogger <ApplicationDbContextFactory> >(); _configuration = GetService <IConfiguration>(); Console.WriteLine(); Console.WriteLine(); _logger.LogInformation( "Initialized '{type}' in environment '{environment}' with connection string set to '{connectionString}'.", nameof(ApplicationDbContextFactory), GetCurrentEnvironment(), GetConnectionString()); }