diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/AbpBackgroundWorkersHangfireModule.cs b/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/AbpBackgroundWorkersHangfireModule.cs index 19bc14a054..a17f2bd6a7 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/AbpBackgroundWorkersHangfireModule.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/AbpBackgroundWorkersHangfireModule.cs @@ -10,7 +10,7 @@ namespace Volo.Abp.BackgroundWorkers.Hangfire; [DependsOn( typeof(AbpBackgroundWorkersModule), typeof(AbpHangfireModule))] -public class AbpBackgroundWorkerHangfireModule : AbpModule +public class AbpBackgroundWorkersHangfireModule : AbpModule { public override void OnPreApplicationInitialization(ApplicationInitializationContext context) { diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs b/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs index ab1cd461e7..33e0ebde4c 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs @@ -21,7 +21,7 @@ public class HangfireBackgroundWorkerManager : IBackgroundWorkerManager, ISingle return Task.CompletedTask; } - public async Task AddAsync(IBackgroundWorker worker) + public Task AddAsync(IBackgroundWorker worker) { if (worker is IHangfireBackgroundWorker hangfireBackgroundWorker) { @@ -42,18 +42,26 @@ public class HangfireBackgroundWorkerManager : IBackgroundWorkerManager, ISingle if (worker is AsyncPeriodicBackgroundWorkerBase or PeriodicBackgroundWorkerBase) { - var timer = (AbpAsyncTimer) worker.GetType() + var timer = worker.GetType() .GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker); - period = timer?.Period; + + if (worker is AsyncPeriodicBackgroundWorkerBase) + { + period = ((AbpAsyncTimer)timer)?.Period; + } + else + { + period = ((AbpTimer)timer)?.Period; + } } else { - return; + return Task.CompletedTask; } if (period == null) { - return; + return Task.CompletedTask; } var adapterType = typeof(HangfirePeriodicBackgroundWorkerAdapter<>).MakeGenericType(worker.GetType()); @@ -61,6 +69,8 @@ public class HangfireBackgroundWorkerManager : IBackgroundWorkerManager, ISingle RecurringJob.AddOrUpdate(() => workerAdapter.DoWorkAsync(), GetCron(period.Value)); } + + return Task.CompletedTask; } protected virtual string GetCron(int period) @@ -82,7 +92,7 @@ public class HangfireBackgroundWorkerManager : IBackgroundWorkerManager, ISingle } else { - cron = $"0 0 */{time.TotalDays} * *"; + throw new AbpException($"Cannot convert period: {period} to cron expression, use HangfireBackgroundWorkerBase to define worker"); } return cron; diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs index 7c32dba467..7361fdfeb9 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzPeriodicBackgroundWorkerAdapter.cs @@ -28,15 +28,23 @@ public class QuartzPeriodicBackgroundWorkerAdapter : QuartzBackgroundWo int? period; var workerType = worker.GetType(); - if (worker is AsyncPeriodicBackgroundWorkerBase || worker is PeriodicBackgroundWorkerBase) + if (worker is AsyncPeriodicBackgroundWorkerBase or PeriodicBackgroundWorkerBase) { if (typeof(TWorker) != worker.GetType()) { throw new ArgumentException($"{nameof(worker)} type is different from the generic type"); } - var timer = (AbpAsyncTimer)worker.GetType().GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker); - period = timer?.Period; + var timer = worker.GetType().GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker); + + if (worker is AsyncPeriodicBackgroundWorkerBase) + { + period = ((AbpAsyncTimer)timer)?.Period; + } + else + { + period = ((AbpTimer)timer)?.Period; + } } else { @@ -60,26 +68,26 @@ public class QuartzPeriodicBackgroundWorkerAdapter : QuartzBackgroundWo public override async Task Execute(IJobExecutionContext context) { - var worker = (IBackgroundWorker)ServiceProvider.GetService(typeof(TWorker)); + var worker = (IBackgroundWorker) ServiceProvider.GetService(typeof(TWorker)); var workerContext = new PeriodicBackgroundWorkerContext(ServiceProvider); switch (worker) { case AsyncPeriodicBackgroundWorkerBase asyncWorker: + { + if (_doWorkAsyncMethod != null) { - if (_doWorkAsyncMethod != null) - { - await (Task)_doWorkAsyncMethod.Invoke(asyncWorker, new object[] { workerContext }); - } - - break; + await (Task) _doWorkAsyncMethod.Invoke(asyncWorker, new object[] {workerContext}); } + + break; + } case PeriodicBackgroundWorkerBase syncWorker: - { - _doWorkMethod?.Invoke(syncWorker, new object[] { workerContext }); + { + _doWorkMethod?.Invoke(syncWorker, new object[] {workerContext}); - break; - } + break; + } } } } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Options/PreConfigureActionList.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Options/PreConfigureActionList.cs index 55d16b34dd..bf8bb90f55 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Options/PreConfigureActionList.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Options/PreConfigureActionList.cs @@ -12,4 +12,11 @@ public class PreConfigureActionList : List> action(options); } } + + public TOptions Configure() + { + var options = Activator.CreateInstance(); + Configure(options); + return options; + } } diff --git a/framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/AbpEventBusRebusModule.cs b/framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/AbpEventBusRebusModule.cs index db819093a6..657c34d2be 100644 --- a/framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/AbpEventBusRebusModule.cs +++ b/framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/AbpEventBusRebusModule.cs @@ -11,18 +11,17 @@ public class AbpEventBusRebusModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { - var options = context.Services.ExecutePreConfiguredActions(); ; - context.Services.AddTransient(typeof(IHandleMessages<>), typeof(RebusDistributedEventHandlerAdapter<>)); + var preActions = context.Services.GetPreConfigureActions(); Configure(rebusOptions => { - context.Services.ExecutePreConfiguredActions(rebusOptions); + preActions.Configure(rebusOptions); }); context.Services.AddRebus(configure => { - options.Configurer?.Invoke(configure); + preActions.Configure().Configurer?.Invoke(configure); return configure; }); } @@ -36,4 +35,4 @@ public class AbpEventBusRebusModule : AbpModule .GetRequiredService() .Initialize(); } -} +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.HangFire/Volo/Abp/Hangfire/AbpHangfireModule.cs b/framework/src/Volo.Abp.HangFire/Volo/Abp/Hangfire/AbpHangfireModule.cs index b40871e82a..b6de8013b1 100644 --- a/framework/src/Volo.Abp.HangFire/Volo/Abp/Hangfire/AbpHangfireModule.cs +++ b/framework/src/Volo.Abp.HangFire/Volo/Abp/Hangfire/AbpHangfireModule.cs @@ -13,9 +13,10 @@ public class AbpHangfireModule : AbpModule public override void ConfigureServices(ServiceConfigurationContext context) { + var preActions = context.Services.GetPreConfigureActions(); context.Services.AddHangfire(configuration => { - context.Services.ExecutePreConfiguredActions(configuration); + preActions.Configure(configuration); }); } diff --git a/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs b/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs index d797b9dfa4..77f791cd27 100644 --- a/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs +++ b/framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs @@ -16,12 +16,11 @@ public class AbpJsonModule : AbpModule context.Services.TryAddEnumerable(ServiceDescriptor .Transient, AbpSystemTextJsonSerializerOptionsSetup>()); + var preActions = context.Services.GetPreConfigureActions(); Configure(options => { options.Providers.Add(); - - var abpJsonOptions = context.Services.ExecutePreConfiguredActions(); - if (abpJsonOptions.UseHybridSerializer) + if (preActions.Configure().UseHybridSerializer) { options.Providers.Add(); } diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Versioning.Tests/Volo/Abp/AspNetCore/Mvc/Versioning/AbpAspNetCoreMvcVersioningTestModule.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Versioning.Tests/Volo/Abp/AspNetCore/Mvc/Versioning/AbpAspNetCoreMvcVersioningTestModule.cs index 0bedc4d049..a8d2a4ff70 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Versioning.Tests/Volo/Abp/AspNetCore/Mvc/Versioning/AbpAspNetCoreMvcVersioningTestModule.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Versioning.Tests/Volo/Abp/AspNetCore/Mvc/Versioning/AbpAspNetCoreMvcVersioningTestModule.cs @@ -38,9 +38,10 @@ public class AbpAspNetCoreMvcVersioningTestModule : AbpModule public override void ConfigureServices(ServiceConfigurationContext context) { + var preActions = context.Services.GetPreConfigureActions(); Configure(options => { - context.Services.ExecutePreConfiguredActions(options); + preActions.Configure(options); }); context.Services.AddAbpApiVersioning(options => @@ -48,11 +49,10 @@ public class AbpAspNetCoreMvcVersioningTestModule : AbpModule options.ReportApiVersions = true; options.AssumeDefaultVersionWhenUnspecified = true; - //options.ApiVersionReader = new HeaderApiVersionReader("api-version"); //Supports header too - //options.ApiVersionReader = new MediaTypeApiVersionReader(); //Supports accept header too + //options.ApiVersionReader = new HeaderApiVersionReader("api-version"); //Supports header too + //options.ApiVersionReader = new MediaTypeApiVersionReader(); //Supports accept header too - var mvcOptions = context.Services.ExecutePreConfiguredActions(); - options.ConfigureAbp(mvcOptions); + options.ConfigureAbp(preActions.Configure()); }); context.Services.AddHttpClientProxies(typeof(AbpAspNetCoreMvcVersioningTestModule).Assembly);