Exemplo n.º 1
0
        public static IApplicationBuilder UseHangfireServer(
            [NotNull] this IApplicationBuilder app,
            [CanBeNull] BackgroundJobServerOptions options = null,
            [CanBeNull] IEnumerable <IBackgroundProcess> additionalProcesses = null,
            [CanBeNull] JobStorage storage = null)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            HangfireServiceCollectionExtensions.ThrowIfNotConfigured(app.ApplicationServices);

            var services = app.ApplicationServices;
            var lifetime = services.GetRequiredService <IApplicationLifetime>();

            storage             = storage ?? services.GetRequiredService <JobStorage>();
            options             = options ?? services.GetService <BackgroundJobServerOptions>() ?? new BackgroundJobServerOptions();
            additionalProcesses = additionalProcesses ?? services.GetServices <IBackgroundProcess>();

            options.Activator        = options.Activator ?? services.GetService <JobActivator>();
            options.FilterProvider   = options.FilterProvider ?? services.GetService <IJobFilterProvider>();
            options.TimeZoneResolver = options.TimeZoneResolver ?? services.GetService <ITimeZoneResolver>();

            var server = HangfireServiceCollectionExtensions.GetInternalServices(services, out var factory, out var stateChanger, out var performer)
#pragma warning disable 618
                ? new BackgroundJobServer(options, storage, additionalProcesses, null, null, factory, performer, stateChanger)
#pragma warning restore 618
                : new BackgroundJobServer(options, storage, additionalProcesses);

            lifetime.ApplicationStopping.Register(() => server.SendStop());
            lifetime.ApplicationStopped.Register(() => server.Dispose());

            return(app);
        }
Exemplo n.º 2
0
        public IBackgroundJobClient GetClient(JobStorage storage)
        {
            if (HangfireServiceCollectionExtensions.GetInternalServices(_serviceProvider, out var factory, out var stateChanger, out _))
            {
                return(new BackgroundJobClient(storage, factory, stateChanger));
            }

            return(new BackgroundJobClient(
                       storage,
                       _serviceProvider.GetRequiredService <IJobFilterProvider>()));
        }
Exemplo n.º 3
0
        public IRecurringJobManager GetManager(JobStorage storage)
        {
            if (HangfireServiceCollectionExtensions.GetInternalServices(_serviceProvider, out var factory, out _, out _))
            {
                return(new RecurringJobManager(
                           storage,
                           factory,
                           _serviceProvider.GetRequiredService <ITimeZoneResolver>()));
            }

            return(new RecurringJobManager(
                       storage,
                       _serviceProvider.GetRequiredService <IJobFilterProvider>(),
                       _serviceProvider.GetRequiredService <ITimeZoneResolver>()));
        }