diff --git a/TodoApi/StartupConfig/HangfireMiddlewareExtensions.cs b/TodoApi/StartupConfig/HangfireMiddlewareExtensions.cs index 4a0b0ed..7e89b60 100644 --- a/TodoApi/StartupConfig/HangfireMiddlewareExtensions.cs +++ b/TodoApi/StartupConfig/HangfireMiddlewareExtensions.cs @@ -1,4 +1,5 @@ using Hangfire; +using Microsoft.Extensions.DependencyInjection; using TodoApi.Helpers; namespace TodoApi.StartupConfig; @@ -14,18 +15,23 @@ public static class HangfireMiddlewareExtensions /// The instance. public static IApplicationBuilder UseHangfireJobs(this IApplicationBuilder app) { - var serviceProvider = app.ApplicationServices; - var pingHelper = serviceProvider.GetRequiredService(); + var serviceScopeFactory = app.ApplicationServices.GetRequiredService(); - RecurringJob.AddOrUpdate( - "PingApi", - () => pingHelper.PingApiWithRetry(), - "*/15 * * * *"); // Adjust to "*/5 * * * *" if needed + using (var scope = serviceScopeFactory.CreateScope()) + { + var pingHelper = scope.ServiceProvider.GetRequiredService(); + + RecurringJob.AddOrUpdate( + "PingApi", + () => pingHelper.PingApiWithRetry(), + "*/15 * * * *"); // Adjust to "*/5 * * * *" if needed + + RecurringJob.AddOrUpdate( + "PingDatabase", + () => pingHelper.PingDatabaseWithRetry(), + "*/15 * * * *"); // Adjust to "*/5 * * * *" if needed + } - RecurringJob.AddOrUpdate( - "PingDatabase", - () => pingHelper.PingDatabaseWithRetry(), - "*/15 * * * *"); // Adjust to "*/5 * * * *" if needed return app; } }