From a881195b3beb569d28815b3adeecfe6de165972d Mon Sep 17 00:00:00 2001 From: maliming Date: Sun, 22 Feb 2026 18:00:39 +0800 Subject: [PATCH] refactor: streamline TickerQ initialization and enhance service access methods --- .../infrastructure/background-jobs/tickerq.md | 5 +-- .../background-workers/tickerq.md | 5 +-- .../AbpApplicationBuilderExtensions.cs | 24 ++++++++++++ ...licationInitializationContextExtensions.cs | 38 +++++++++++++++++++ .../Abp/AspNetCore/AbpAspNetCoreModule.cs | 5 +++ .../DemoAppTickerQModule.cs | 3 +- 6 files changed, 73 insertions(+), 7 deletions(-) diff --git a/docs/en/framework/infrastructure/background-jobs/tickerq.md b/docs/en/framework/infrastructure/background-jobs/tickerq.md index 8ac79d7e87..d2f6d55f8c 100644 --- a/docs/en/framework/infrastructure/background-jobs/tickerq.md +++ b/docs/en/framework/infrastructure/background-jobs/tickerq.md @@ -38,14 +38,13 @@ public override void ConfigureServices(ServiceConfigurationContext context) ### UseAbpTickerQ -You need to call the `UseAbpTickerQ` extension method in the `OnApplicationInitialization` method of your module. The method is an extension on `IHost`, so cast the application builder accordingly: +You need to call the `UseAbpTickerQ` extension method in the `OnApplicationInitialization` method of your module: ```csharp public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context) { - var app = context.GetApplicationBuilder(); // (default: TickerQStartMode.Immediate) - (app as IHost)?.UseAbpTickerQ(startMode: ...); + context.GetHost().UseAbpTickerQ(startMode: ...); } ``` diff --git a/docs/en/framework/infrastructure/background-workers/tickerq.md b/docs/en/framework/infrastructure/background-workers/tickerq.md index 8054e10e79..ffe3ca46a6 100644 --- a/docs/en/framework/infrastructure/background-workers/tickerq.md +++ b/docs/en/framework/infrastructure/background-workers/tickerq.md @@ -36,14 +36,13 @@ public override void ConfigureServices(ServiceConfigurationContext context) ### UseAbpTickerQ -You need to call the `UseAbpTickerQ` extension method in the `OnApplicationInitialization` method of your module. The method is an extension on `IHost`, so cast the application builder accordingly: +You need to call the `UseAbpTickerQ` extension method in the `OnApplicationInitialization` method of your module: ```csharp public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context) { - var app = context.GetApplicationBuilder(); // (default: TickerQStartMode.Immediate) - (app as IHost)?.UseAbpTickerQ(startMode: ...); + context.GetHost().UseAbpTickerQ(startMode: ...); } ``` diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs index 3a283cbe36..aab5b5cc2b 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Builder/AbpApplicationBuilderExtensions.cs @@ -38,6 +38,18 @@ public static class AbpApplicationBuilderExtensions Check.NotNull(app, nameof(app)); app.ApplicationServices.GetRequiredService>().Value = app; + if (app is WebApplication webApplication) + { + app.ApplicationServices.GetRequiredService>().Value = webApplication; + } + if (app is IHost host) + { + app.ApplicationServices.GetRequiredService>().Value = host; + } + if (app is IEndpointRouteBuilder endpointRouteBuilder) + { + app.ApplicationServices.GetRequiredService>().Value = endpointRouteBuilder; + } var application = app.ApplicationServices.GetRequiredService(); var applicationLifetime = app.ApplicationServices.GetRequiredService(); @@ -59,6 +71,18 @@ public static class AbpApplicationBuilderExtensions Check.NotNull(app, nameof(app)); app.ApplicationServices.GetRequiredService>().Value = app; + if (app is WebApplication webApplication) + { + app.ApplicationServices.GetRequiredService>().Value = webApplication; + } + if (app is IHost host) + { + app.ApplicationServices.GetRequiredService>().Value = host; + } + if (app is IEndpointRouteBuilder endpointRouteBuilder) + { + app.ApplicationServices.GetRequiredService>().Value = endpointRouteBuilder; + } var application = app.ApplicationServices.GetRequiredService(); var applicationLifetime = app.ApplicationServices.GetRequiredService(); diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/ApplicationInitializationContextExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/ApplicationInitializationContextExtensions.cs index 1e361d1587..930b081df0 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/ApplicationInitializationContextExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/ApplicationInitializationContextExtensions.cs @@ -1,7 +1,9 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Volo.Abp.DependencyInjection; @@ -21,6 +23,42 @@ public static class ApplicationInitializationContextExtensions return context.ServiceProvider.GetRequiredService>().Value; } + public static IHost GetHost(this ApplicationInitializationContext context) + { + var host = context.ServiceProvider.GetRequiredService>().Value; + Check.NotNull(host, nameof(host)); + return host; + } + + public static IHost? GetHostOrNull(this ApplicationInitializationContext context) + { + return context.ServiceProvider.GetRequiredService>().Value; + } + + public static IEndpointRouteBuilder GetEndpointRouteBuilder(this ApplicationInitializationContext context) + { + var endpointRouteBuilder = context.ServiceProvider.GetRequiredService>().Value; + Check.NotNull(endpointRouteBuilder, nameof(endpointRouteBuilder)); + return endpointRouteBuilder; + } + + public static IEndpointRouteBuilder? GetEndpointRouteBuilderOrNull(this ApplicationInitializationContext context) + { + return context.ServiceProvider.GetRequiredService>().Value; + } + + public static WebApplication GetWebApplication(this ApplicationInitializationContext context) + { + var webApplication = context.ServiceProvider.GetRequiredService>().Value; + Check.NotNull(webApplication, nameof(webApplication)); + return webApplication; + } + + public static WebApplication? GetWebApplicationOrNull(this ApplicationInitializationContext context) + { + return context.ServiceProvider.GetRequiredService>().Value; + } + public static IWebHostEnvironment GetEnvironment(this ApplicationInitializationContext context) { return context.ServiceProvider.GetRequiredService(); diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs index 405e49753c..a457d3b1eb 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs @@ -2,7 +2,9 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting.StaticWebAssets; using Microsoft.AspNetCore.RequestLocalization; +using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.FileProviders; using MyCSharp.HttpUserAgentParser.DependencyInjection; using Volo.Abp.AspNetCore.Auditing; @@ -57,6 +59,9 @@ public class AbpAspNetCoreModule : AbpModule AddAspNetServices(context.Services); context.Services.AddObjectAccessor(); + context.Services.AddObjectAccessor(); + context.Services.AddObjectAccessor(); + context.Services.AddObjectAccessor(); context.Services.AddAbpDynamicOptions(); StaticWebAssetsLoader.UseStaticWebAssets(context.Services.GetHostingEnvironment(), context.Services.GetConfiguration()); diff --git a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/DemoAppTickerQModule.cs b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/DemoAppTickerQModule.cs index d13e39db97..de3ef3c05d 100644 --- a/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/DemoAppTickerQModule.cs +++ b/modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.TickerQ/DemoAppTickerQModule.cs @@ -94,7 +94,8 @@ public class DemoAppTickerQModule : AbpModule await backgroundWorkerManager.AddAsync(context.ServiceProvider.GetRequiredService()); var app = context.GetApplicationBuilder(); - (app as IHost)?.UseAbpTickerQ(); + + context.GetHost().UseAbpTickerQ(); var timeTickerManager = context.ServiceProvider.GetRequiredService>(); await timeTickerManager.AddAsync(new TimeTickerEntity