From d765faaf30838c1a7a960d0b6e17bb5d66752caf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Wed, 7 Dec 2022 14:24:17 +0100 Subject: [PATCH] Add IBackgroundJobClientFactoryV2 and IRecurringJobManagerFactoryV2 In order to make `IBackgroundJobClientV2` and `IRecurringJobManagerV2` injectable. --- .../DefaultClientManagerFactory.cs | 16 +++++++++++++--- .../HangfireServiceCollectionExtensions.cs | 6 ++++++ .../IBackgroundJobClientFactory.cs | 5 +++++ .../IRecurringJobManagerFactory.cs | 5 +++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Hangfire.NetCore/DefaultClientManagerFactory.cs b/src/Hangfire.NetCore/DefaultClientManagerFactory.cs index 46f1a16cc..0aca09be2 100644 --- a/src/Hangfire.NetCore/DefaultClientManagerFactory.cs +++ b/src/Hangfire.NetCore/DefaultClientManagerFactory.cs @@ -20,7 +20,7 @@ namespace Hangfire { - internal sealed class DefaultClientManagerFactory : IBackgroundJobClientFactory, IRecurringJobManagerFactory + internal sealed class DefaultClientManagerFactory : IBackgroundJobClientFactoryV2, IRecurringJobManagerFactoryV2 { private readonly IServiceProvider _serviceProvider; @@ -29,7 +29,7 @@ public DefaultClientManagerFactory([NotNull] IServiceProvider serviceProvider) _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); } - public IBackgroundJobClient GetClient(JobStorage storage) + public IBackgroundJobClientV2 GetClientV2(JobStorage storage) { if (HangfireServiceCollectionExtensions.GetInternalServices(_serviceProvider, out var factory, out var stateChanger, out _)) { @@ -41,7 +41,12 @@ public IBackgroundJobClient GetClient(JobStorage storage) _serviceProvider.GetRequiredService()); } - public IRecurringJobManager GetManager(JobStorage storage) + public IBackgroundJobClient GetClient(JobStorage storage) + { + return GetClientV2(storage); + } + + public IRecurringJobManagerV2 GetManagerV2(JobStorage storage) { if (HangfireServiceCollectionExtensions.GetInternalServices(_serviceProvider, out var factory, out _, out _)) { @@ -56,5 +61,10 @@ public IRecurringJobManager GetManager(JobStorage storage) _serviceProvider.GetRequiredService(), _serviceProvider.GetRequiredService()); } + + public IRecurringJobManager GetManager(JobStorage storage) + { + return GetManagerV2(storage); + } } } \ No newline at end of file diff --git a/src/Hangfire.NetCore/HangfireServiceCollectionExtensions.cs b/src/Hangfire.NetCore/HangfireServiceCollectionExtensions.cs index fa6b8046f..e4ff4b9b5 100644 --- a/src/Hangfire.NetCore/HangfireServiceCollectionExtensions.cs +++ b/src/Hangfire.NetCore/HangfireServiceCollectionExtensions.cs @@ -56,13 +56,19 @@ public static IServiceCollection AddHangfire( services.TryAddSingleton(x => new DefaultClientManagerFactory(x)); services.TryAddSingletonChecked(x => x.GetService()); + services.TryAddSingletonChecked(x => x.GetService()); services.TryAddSingletonChecked(x => x.GetService()); + services.TryAddSingletonChecked(x => x.GetService()); services.TryAddSingletonChecked(x => x .GetService().GetClient(x.GetService())); + services.TryAddSingletonChecked(x => x + .GetService().GetClientV2(x.GetService())); services.TryAddSingletonChecked(x => x .GetService().GetManager(x.GetService())); + services.TryAddSingletonChecked(x => x + .GetService().GetManagerV2(x.GetService())); // IGlobalConfiguration serves as a marker indicating that Hangfire's services // were added to the service container (checked by IApplicationBuilder extensions). diff --git a/src/Hangfire.NetCore/IBackgroundJobClientFactory.cs b/src/Hangfire.NetCore/IBackgroundJobClientFactory.cs index efde134ec..abb5079e5 100644 --- a/src/Hangfire.NetCore/IBackgroundJobClientFactory.cs +++ b/src/Hangfire.NetCore/IBackgroundJobClientFactory.cs @@ -15,6 +15,11 @@ namespace Hangfire { + public interface IBackgroundJobClientFactoryV2 : IBackgroundJobClientFactory + { + IBackgroundJobClientV2 GetClientV2(JobStorage storage); + } + public interface IBackgroundJobClientFactory { IBackgroundJobClient GetClient(JobStorage storage); diff --git a/src/Hangfire.NetCore/IRecurringJobManagerFactory.cs b/src/Hangfire.NetCore/IRecurringJobManagerFactory.cs index b306a7b67..6d51db8e3 100644 --- a/src/Hangfire.NetCore/IRecurringJobManagerFactory.cs +++ b/src/Hangfire.NetCore/IRecurringJobManagerFactory.cs @@ -15,6 +15,11 @@ namespace Hangfire { + public interface IRecurringJobManagerFactoryV2 : IRecurringJobManagerFactory + { + IRecurringJobManagerV2 GetManagerV2(JobStorage storage); + } + public interface IRecurringJobManagerFactory { IRecurringJobManager GetManager(JobStorage storage);