Пример #1
0
        public static IServiceCollection AddQuartz(
            this IServiceCollection services,
            Action <IServiceCollectionQuartzConfigurator>?configure = null)
        {
            var builder = new ServiceCollectionQuartzConfigurator(services, SchedulerBuilder.Create());

            configure?.Invoke(builder);

            services.AddSingleton <ISchedulerFactory>(serviceProvider => new ServiceCollectionSchedulerFactory(serviceProvider, builder));
            return(services);
        }
Пример #2
0
        /// <summary>
        /// Configures Quartz services to underlying service collection. This API maybe change!
        /// </summary>
        public static IServiceCollection AddQuartz(
            this IServiceCollection services,
            NameValueCollection properties,
            Action <IServiceCollectionQuartzConfigurator>?configure = null)
        {
            services.AddOptions();
            services.TryAddSingleton <MicrosoftLoggingProvider?>(serviceProvider =>
            {
                var loggerFactory = serviceProvider.GetService <ILoggerFactory>();

                if (loggerFactory is null)
                {
                    throw new InvalidOperationException($"{nameof(ILoggerFactory)} service is required");
                }

                LogContext.SetCurrentLogProvider(loggerFactory);

                return(LogProvider.CurrentLogProvider as MicrosoftLoggingProvider);
            });

            // Note that we can't call UseSimpleTypeLoader(), as that would overwrite any other configured type loaders
            services.TryAddSingleton(typeof(ITypeLoadHelper), typeof(SimpleTypeLoadHelper));

            var schedulerBuilder = SchedulerBuilder.Create(properties);

            if (configure != null)
            {
                var target = new ServiceCollectionQuartzConfigurator(services, schedulerBuilder);
                configure(target);
            }

            services.Configure <QuartzOptions>(options =>
            {
                foreach (var key in schedulerBuilder.Properties.AllKeys)
                {
                    options[key] = schedulerBuilder.Properties[key];
                }
            });

            services.TryAddSingleton <ContainerConfigurationProcessor>();
            services.TryAddSingleton <ISchedulerFactory, ServiceCollectionSchedulerFactory>();

            // Note: TryAddEnumerable() is used here to ensure the initializers are registered only once.
            services.TryAddEnumerable(new []
            {
                ServiceDescriptor.Singleton <IPostConfigureOptions <QuartzOptions>, QuartzConfiguration>()
            });

            return(services);
        }
        /// <summary>
        /// Configures Quartz services to underlying service collection. This API maybe change!
        /// </summary>
        public static IServiceCollection AddQuartz(
            this IServiceCollection services,
            Action <IServiceCollectionQuartzConfigurator>?configure = null)
        {
            services.TryAddSingleton <MicrosoftLoggingProvider?>(serviceProvider =>
            {
                var loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>();
                if (loggerFactory != null)
                {
                    LogContext.SetCurrentLogProvider(loggerFactory);
                }

                return(LogProvider.CurrentLogProvider as MicrosoftLoggingProvider);
            });

            var builder = new ServiceCollectionQuartzConfigurator(services, SchedulerBuilder.Create());

            configure?.Invoke(builder);

            // Note that we can't call UseSimpleTypeLoader(), as that would overwrite any other configured type loaders
            services.TryAddSingleton(typeof(ITypeLoadHelper), typeof(SimpleTypeLoadHelper));

            services.AddSingleton <ISchedulerFactory>(serviceProvider =>
            {
                // try standard appsettings.json
                var config  = serviceProvider.GetService <IConfiguration>();
                var section = config.GetSection("Quartz");
                var options = new NameValueCollection();

                foreach (var kvp in section.GetChildren())
                {
                    options.Set(kvp.Key, kvp.Value);
                }

                // now override with programmatic configuration
                foreach (string?key in builder.Properties.Keys)
                {
                    options.Set(key, builder.Properties[key]);
                }

                return(new ServiceCollectionSchedulerFactory(serviceProvider, options));
            });
            return(services);
        }
Пример #4
0
        /// <summary>
        /// Configures Quartz services to underlying service collection. This API maybe change!
        /// </summary>
        public static IServiceCollection AddQuartz(
            this IServiceCollection services,
            NameValueCollection properties,
            Action <IServiceCollectionQuartzConfigurator>?configure = null)
        {
            services.AddOptions();
            services.TryAddSingleton <MicrosoftLoggingProvider?>(serviceProvider =>
            {
                var loggerFactory = serviceProvider.GetService <ILoggerFactory>();

                if (loggerFactory is null)
                {
                    throw new InvalidOperationException($"{nameof(ILoggerFactory)} service is required");
                }

                LogContext.SetCurrentLogProvider(loggerFactory);

                return(LogProvider.CurrentLogProvider as MicrosoftLoggingProvider);
            });

            var schedulerBuilder = SchedulerBuilder.Create(properties);

            if (configure != null)
            {
                var target = new ServiceCollectionQuartzConfigurator(services, schedulerBuilder);
                configure(target);
            }


            // try to add services if not present with defaults, without overriding other configuration
            if (string.IsNullOrWhiteSpace(properties[StdSchedulerFactory.PropertySchedulerTypeLoadHelperType]))
            {
                services.TryAddSingleton(typeof(ITypeLoadHelper), typeof(SimpleTypeLoadHelper));
            }

            var allowDefaultConstructor = false;

            if (string.IsNullOrWhiteSpace(properties[StdSchedulerFactory.PropertySchedulerJobFactoryType]))
            {
                // there's no explicit job factory defined, use MS version and allow default constructor
                services.TryAddSingleton(typeof(IJobFactory), typeof(MicrosoftDependencyInjectionJobFactory));
                allowDefaultConstructor = true;
            }

            services.Configure <QuartzOptions>(options =>
            {
                foreach (var key in schedulerBuilder.Properties.AllKeys)
                {
                    options[key] = schedulerBuilder.Properties[key];
                }

                if (allowDefaultConstructor)
                {
                    options.JobFactory.AllowDefaultConstructor = true;
                }
            });

            services.TryAddSingleton <ContainerConfigurationProcessor>();
            services.TryAddSingleton <ISchedulerFactory, ServiceCollectionSchedulerFactory>();

            // Note: TryAddEnumerable() is used here to ensure the initializers are registered only once.
            services.TryAddEnumerable(new[]
            {
                ServiceDescriptor.Singleton <IPostConfigureOptions <QuartzOptions>, QuartzConfiguration>()
            });

            return(services);
        }