Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Add new appsettings - DisableHostedService - it allow to disable Hosted Services (BackgroundServiceTask) - useful settings when you use Load Balancer

KrzysztofPajak4ddd61c74454
diff --git a/src/Core/Grand.Infrastructure/Configuration/AppConfig.cs b/src/Core/Grand.Infrastructure/Configuration/AppConfig.cs
index a1c5eb70b..2d9f4b179 100644
--- a/src/Core/Grand.Infrastructure/Configuration/AppConfig.cs
+++ b/src/Core/Grand.Infrastructure/Configuration/AppConfig.cs
@@ -22,6 +22,11 @@ namespace Grand.Infrastructure.Configuration
/// </summary>
public bool IgnoreStartupTasks { get; set; }
+ /// <summary>
+ /// Indicates whether we should Disable HostedService - BackgroundServiceTask
+ /// </summary>
+ public bool DisableHostedService { get; set; }
+
/// <summary>
/// A value indicating whether to ignore ACL rules (side-wide). It can significantly improve performance when enabled.
/// </summary>
diff --git a/src/Web/Grand.Web.Common/Startup/TaskHandlerHelper.cs b/src/Web/Grand.Web.Common/Startup/TaskHandlerHelper.cs
index 84ef1a730..7eacf5f8a 100644
--- a/src/Web/Grand.Web.Common/Startup/TaskHandlerHelper.cs
+++ b/src/Web/Grand.Web.Common/Startup/TaskHandlerHelper.cs
@@ -1,6 +1,7 @@
using Grand.Business.System.Interfaces.ScheduleTasks;
using Grand.Business.System.Services.BackgroundServices;
using Grand.Domain.Data;
+using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Plugins;
using Grand.Infrastructure.TypeSearchers;
using Microsoft.Extensions.DependencyInjection;
@@ -14,23 +15,26 @@ namespace Grand.Web.Common.Startup
public static void RegisterTasks(this IServiceCollection services)
{
-
//database is already installed, so start scheduled tasks
if (DataSettingsManager.DatabaseIsInstalled())
{
- var typeSearcher = new AppTypeSearcher();
- var scheduleTasks = typeSearcher.ClassesOfType<IScheduleTask>();
+ var appConfig = services.BuildServiceProvider().GetRequiredService<AppConfig>();
+ if (!appConfig.DisableHostedService)
+ {
+ var typeSearcher = new AppTypeSearcher();
+ var scheduleTasks = typeSearcher.ClassesOfType<IScheduleTask>();
- var scheduleTasksInstalled = scheduleTasks
- .Where(t => PluginExtensions.OnlyInstalledPlugins(t)); //ignore not installed plugins
+ var scheduleTasksInstalled = scheduleTasks
+ .Where(t => PluginExtensions.OnlyInstalledPlugins(t)); //ignore not installed plugins
- foreach (var task in scheduleTasksInstalled)
- {
- var assemblyName = task.Assembly.GetName().Name;
- services.AddSingleton<IHostedService, BackgroundServiceTask>(sp =>
+ foreach (var task in scheduleTasksInstalled)
{
- return new BackgroundServiceTask($"{task.FullName}, {assemblyName}", sp);
- });
+ var assemblyName = task.Assembly.GetName().Name;
+ services.AddSingleton<IHostedService, BackgroundServiceTask>(sp =>
+ {
+ return new BackgroundServiceTask($"{task.FullName}, {assemblyName}", sp);
+ });
+ }
}
}
}
diff --git a/src/Web/Grand.Web/App_Data/appsettings.json b/src/Web/Grand.Web/App_Data/appsettings.json
index 0b375299a..375ff0388 100644
--- a/src/Web/Grand.Web/App_Data/appsettings.json
+++ b/src/Web/Grand.Web/App_Data/appsettings.json
@@ -147,6 +147,8 @@
"SupportedCultures": [ "en-US", "pl-PL" ],
//Indicates whether to ignore startup tasks
"IgnoreStartupTasks": true,
+ //Indicates whether to disable hosted service - BackgroundServiceTask
+ "DisableHostedService": false,
//Enable if you want to clear /Plugins/bin directory on application startup
"ClearPluginShadowDirectoryOnStartup": true,
//For develop you should enable if you want to copy plugin dll files to directory /Plugins/bin on application startup