diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AbpBackgroundWorkersModule.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AbpBackgroundWorkersModule.cs index 12e24c8f81..b93a5e9353 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AbpBackgroundWorkersModule.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AbpBackgroundWorkersModule.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using Volo.Abp.Modularity; using Volo.Abp.Threading; @@ -16,9 +17,10 @@ public class AbpBackgroundWorkersModule : AbpModule var options = context.ServiceProvider.GetRequiredService>().Value; if (options.IsEnabled) { + var hostApplicationLifetime = context.ServiceProvider.GetRequiredService(); await context.ServiceProvider .GetRequiredService() - .StartAsync(); + .StartAsync(hostApplicationLifetime.ApplicationStopping); } } @@ -27,9 +29,10 @@ public class AbpBackgroundWorkersModule : AbpModule var options = context.ServiceProvider.GetRequiredService>().Value; if (options.IsEnabled) { + var hostApplicationLifetime = context.ServiceProvider.GetRequiredService(); await context.ServiceProvider .GetRequiredService() - .StopAsync(); + .StopAsync(hostApplicationLifetime.ApplicationStopping); } } diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs index 652aa35741..6a362b44ae 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs @@ -3,6 +3,7 @@ using System.Threading; using System.Threading.Tasks; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; namespace Volo.Abp.BackgroundWorkers; @@ -28,6 +29,12 @@ public static class BackgroundWorkersApplicationInitializationContextExtensions throw new AbpException($"Given type ({workerType.AssemblyQualifiedName}) must implement the {typeof(IBackgroundWorker).AssemblyQualifiedName} interface, but it doesn't!"); } + if (cancellationToken == default) + { + var hostApplicationLifetime = context.ServiceProvider.GetRequiredService(); + cancellationToken = hostApplicationLifetime.ApplicationStopping; + } + await context.ServiceProvider .GetRequiredService() .AddAsync((IBackgroundWorker)context.ServiceProvider.GetRequiredService(workerType), cancellationToken);