Exemple #1
0
        static void Main()
        {
            var host = new HostBuilder()
                       .ConfigureHostConfiguration(configHost =>
            {
                configHost.SetBasePath(Directory.GetCurrentDirectory());
                //configHost.AddJsonFile("hostsettings.json", true, true);
                configHost.AddEnvironmentVariables("ASPNETCORE_");
                //configHost.AddCommandLine(args);
            })
                       .ConfigureAppConfiguration((hostContext, configApp) =>
            {
                configApp.AddJsonFile("appsettings.json", true);
                configApp.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", true);
                configApp.AddEnvironmentVariables();
                //configApp.AddCommandLine(args);
            })
                       .ConfigureServices((hostContext, services) =>
            {
                services.AddLogging();
                //services.AddHostedService<TimedHostedService>();

                services.AddSingleton <IJobFactory, JobFactory>();
                services.AddSingleton(provider =>
                {
                    var option           = new QuartzOption(hostContext.Configuration);
                    var sf               = new StdSchedulerFactory(option.ToProperties());
                    var scheduler        = sf.GetScheduler().Result;
                    scheduler.JobFactory = provider.GetService <IJobFactory>();
                    return(scheduler);
                });
                services.AddHostedService <QuartzService>();

                services.AddSingleton <TestJob, TestJob>();
                services.AddSingleton <TestJobTwo, TestJobTwo>();
            })
                       .ConfigureLogging((hostContext, configLogging) =>
            {
                configLogging.AddConsole();
                if (hostContext.HostingEnvironment.EnvironmentName == EnvironmentName.Development)
                {
                    configLogging.AddDebug();
                }
            })
                       .UseConsoleLifetime()
                       .Build();

            host.Run();
        }
Exemple #2
0
        static void Main()
        {
            var host = new HostBuilder()
                       .ConfigureHostConfiguration(configHost =>
            {
                configHost.SetBasePath(Directory.GetCurrentDirectory());
                configHost.AddEnvironmentVariables("ASPNETCORE_");
                //configHost.AddCommandLine(args);
            })
                       .ConfigureAppConfiguration((hostContext, configApp) =>
            {
                configApp.AddJsonFile("appsettings.json", true, true);
                configApp.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", true, true);
                configApp.AddEnvironmentVariables();
            })
                       .ConfigureServices((hostContext, services) =>
            {
                services.AddDbContext <GameBackendContext>(options =>
                                                           options.UseMySql(hostContext.Configuration.GetConnectionString("MySql_GameBackend")));

                services.AddLogging();
                var serviceProvider = services.BuildServiceProvider();
                var loggerFactory   = serviceProvider.GetService <ILoggerFactory>();
                loggerFactory.AddNLog();

                services.AddSingleton <IJobFactory, JobFactory>();
                services.AddHostedService <QuartzService>();
                services.AddSingleton(provider =>
                {
                    var option           = new QuartzOption(hostContext.Configuration);
                    var sf               = new StdSchedulerFactory(option.ToProperties());
                    var scheduler        = sf.GetScheduler().Result;
                    scheduler.JobFactory = provider.GetService <IJobFactory>();
                    return(scheduler);
                });

                var jobs = typeof(Program).Assembly.GetTypes().Where(x => x.GetInterfaces().Contains(typeof(IJob))).ToList();
                foreach (var job in jobs)
                {
                    services.AddSingleton(job);
                    //services.AddSingleton<ajob, ajob>();
                }
            })
                       .UseConsoleLifetime()
                       .Build();

            host.Run();
        }