From 436b81cee64fc2a2319d4f7188bcb84a22607e31 Mon Sep 17 00:00:00 2001 From: feijie Date: Fri, 21 Mar 2025 19:21:55 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(host):=20=E6=B7=BB=E5=8A=A0=20?= =?UTF-8?q?Hangfire=20=E6=94=AF=E6=8C=81=EF=BC=8C=E5=B9=B6=E5=90=AF?= =?UTF-8?q?=E7=94=A8=E4=BB=AA=E8=A1=A8=E7=9B=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为应用添加后台任务调度框架 Hangfire,并启用其仪表盘,以便监视和管理后台任务。 --- ...rviceApplicationsSingleModule.Configure.cs | 71 +-- .../MicroServiceApplicationsSingleModule.cs | 91 +--- ...me.CompanyName.ProjectName.AIO.Host.csproj | 494 +++++++++--------- .../Program.cs | 1 + 4 files changed, 283 insertions(+), 374 deletions(-) diff --git a/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/MicroServiceApplicationsSingleModule.Configure.cs b/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/MicroServiceApplicationsSingleModule.Configure.cs index 765a6ceb2..fe7548c03 100644 --- a/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/MicroServiceApplicationsSingleModule.Configure.cs +++ b/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/MicroServiceApplicationsSingleModule.Configure.cs @@ -1,8 +1,8 @@ using Elsa; using Elsa.Options; +using Hangfire; +using Hangfire.Redis.StackExchange; using LINGYUN.Abp.Aliyun.Localization; -using LINGYUN.Abp.BackgroundTasks; -using LINGYUN.Abp.DataProtectionManagement; using LINGYUN.Abp.ExceptionHandling; using LINGYUN.Abp.ExceptionHandling.Emailing; using LINGYUN.Abp.Exporter.MiniExcel; @@ -29,7 +29,6 @@ using LINGYUN.Abp.WeChat.Work; using LINGYUN.Abp.Wrapper; using LINGYUN.Platform.Localization; using PackageName.CompanyName.ProjectName.AIO.Host.Microsoft.Extensions.DependencyInjection; -using PackageName.CompanyName.ProjectName.EntityFrameworkCore; using Medallion.Threading; using Medallion.Threading.Redis; using Microsoft.AspNetCore.Authentication.Cookies; @@ -41,13 +40,11 @@ using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.Caching.StackExchangeRedis; using Microsoft.IdentityModel.Logging; using Microsoft.OpenApi.Models; -using MiniExcelLibs.Attributes; using OpenIddict.Server; using OpenIddict.Server.AspNetCore; using PackageName.CompanyName.ProjectName.AIO.Host.Authentication; using PackageName.CompanyName.ProjectName.AIO.Host.IdentityResources; using PackageName.CompanyName.ProjectName.AIO.Host.WeChat.Official.Messages; -using Quartz; using StackExchange.Redis; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; @@ -56,9 +53,11 @@ using System.Text.Unicode; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.AntiForgery; +using Volo.Abp.AspNetCore.Mvc.Libs; using Volo.Abp.AspNetCore.Mvc.UI.Bundling; using Volo.Abp.Auditing; using Volo.Abp.Authorization.Permissions; +using Volo.Abp.BackgroundWorkers; using Volo.Abp.BlobStoring; using Volo.Abp.BlobStoring.FileSystem; using Volo.Abp.Caching; @@ -77,7 +76,6 @@ using Volo.Abp.MultiTenancy; using Volo.Abp.OpenIddict; using Volo.Abp.OpenIddict.Localization; using Volo.Abp.PermissionManagement; -using Volo.Abp.Quartz; using Volo.Abp.Security.Claims; using Volo.Abp.SettingManagement; using Volo.Abp.SettingManagement.Localization; @@ -232,34 +230,6 @@ public partial class MicroServiceApplicationsSingleModule } } - private void PreConfigureQuartz(IConfiguration configuration) - { - PreConfigure(options => - { - // 如果使用持久化存储, 则配置quartz持久层 - if (configuration.GetSection("Quartz:UsePersistentStore").Get()) - { - var settings = configuration.GetSection("Quartz:Properties").Get>(); - if (settings != null) - { - foreach (var setting in settings) - { - options.Properties[setting.Key] = setting.Value; - } - } - - options.Configurator += (config) => - { - config.UsePersistentStore(store => - { - store.UseProperties = false; - store.UseNewtonsoftJsonSerializer(); - }); - }; - } - }); - } - private void PreConfigureElsa(IServiceCollection services, IConfiguration configuration) { var elsaSection = configuration.GetSection("Elsa"); @@ -406,17 +376,6 @@ public partial class MicroServiceApplicationsSingleModule }); } - private void ConfigureBackgroundTasks() - { - Configure(options => - { - options.NodeName = ApplicationName; - options.JobCleanEnabled = true; - options.JobFetchEnabled = true; - options.JobCheckEnabled = true; - }); - } - private void ConfigureTextTemplating(IConfiguration configuration) { if (configuration.GetValue("TextTemplating:IsDynamicStoreEnabled")) @@ -767,6 +726,28 @@ public partial class MicroServiceApplicationsSingleModule // } //); }); + + Configure(options => + { + options.CheckLibs = false; + }); + } + + private void ConfigureHangfire(IServiceCollection services, IConfiguration configuration) + { + // 配置Hangfire存储和设置 + Configure(options => + { + options.IsEnabled = true; + }); + + var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); + // 配置Hangfire + services.AddHangfire(config => + { + config.UseRedisStorage(redis); + }); + } private void ConfigureLocalization() diff --git a/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/MicroServiceApplicationsSingleModule.cs b/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/MicroServiceApplicationsSingleModule.cs index c3b097cba..578b8eb24 100644 --- a/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/MicroServiceApplicationsSingleModule.cs +++ b/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/MicroServiceApplicationsSingleModule.cs @@ -10,22 +10,11 @@ using LINGYUN.Abp.AuditLogging.EntityFrameworkCore; using LINGYUN.Abp.Authentication.QQ; using LINGYUN.Abp.Authentication.WeChat; using LINGYUN.Abp.Authorization.OrganizationUnits; -using LINGYUN.Abp.BackgroundTasks; -using LINGYUN.Abp.BackgroundTasks.Activities; -using LINGYUN.Abp.BackgroundTasks.DistributedLocking; -using LINGYUN.Abp.BackgroundTasks.EventBus; -using LINGYUN.Abp.BackgroundTasks.ExceptionHandling; -using LINGYUN.Abp.BackgroundTasks.Jobs; -using LINGYUN.Abp.BackgroundTasks.Notifications; -using LINGYUN.Abp.BackgroundTasks.Quartz; using LINGYUN.Abp.CachingManagement; using LINGYUN.Abp.CachingManagement.StackExchangeRedis; -using LINGYUN.Abp.Dapr.Client; using LINGYUN.Abp.Data.DbMigrator; using LINGYUN.Abp.DataProtectionManagement; using LINGYUN.Abp.DataProtectionManagement.EntityFrameworkCore; -// using LINGYUN.Abp.Demo; -// using LINGYUN.Abp.Demo.EntityFrameworkCore; using LINGYUN.Abp.ExceptionHandling; using LINGYUN.Abp.ExceptionHandling.Emailing; using LINGYUN.Abp.Exporter.MiniExcel; @@ -66,7 +55,6 @@ using LINGYUN.Abp.OpenIddict.WeChat; using LINGYUN.Abp.OpenIddict.WeChat.Work; using LINGYUN.Abp.OssManagement; using LINGYUN.Abp.OssManagement.FileSystem; -// using LINGYUN.Abp.OssManagement.Imaging; using LINGYUN.Abp.OssManagement.SettingManagement; using LINGYUN.Abp.PermissionManagement; using LINGYUN.Abp.PermissionManagement.HttpApi; @@ -77,8 +65,6 @@ using LINGYUN.Abp.Serilog.Enrichers.Application; using LINGYUN.Abp.Serilog.Enrichers.UniqueId; using LINGYUN.Abp.SettingManagement; using LINGYUN.Abp.Sms.Aliyun; -using LINGYUN.Abp.TaskManagement; -using LINGYUN.Abp.TaskManagement.EntityFrameworkCore; using LINGYUN.Abp.Tencent.QQ; using LINGYUN.Abp.Tencent.SettingManagement; using LINGYUN.Abp.TextTemplating; @@ -111,8 +97,11 @@ using Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic; using Volo.Abp.AspNetCore.Serilog; using Volo.Abp.Autofac; +using Volo.Abp.BackgroundJobs.Hangfire; +using Volo.Abp.BackgroundWorkers.Hangfire; using Volo.Abp.Caching.StackExchangeRedis; using Volo.Abp.Data; +using Volo.Abp.EntityFrameworkCore.PostgreSql; using Volo.Abp.EventBus; using Volo.Abp.FeatureManagement.EntityFrameworkCore; using Volo.Abp.Imaging; @@ -124,20 +113,9 @@ using Volo.Abp.PermissionManagement.OpenIddict; using Volo.Abp.SettingManagement; using Volo.Abp.SettingManagement.EntityFrameworkCore; using Volo.Abp.Threading; -#if MySQL -using Volo.Abp.EntityFrameworkCore.MySQL; -#elif SqlServer -using Volo.Abp.EntityFrameworkCore.SqlServer; -using Microsoft.EntityFrameworkCore.Infrastructure; -#elif Sqlite -using Volo.Abp.EntityFrameworkCore.Sqlite; -#elif Oracle -using Volo.Abp.EntityFrameworkCore.Oracle; -#elif OracleDevart -using Volo.Abp.EntityFrameworkCore.Oracle.Devart; -#elif PostgreSql -using Volo.Abp.EntityFrameworkCore.PostgreSql; -#endif +// using LINGYUN.Abp.Demo; +// using LINGYUN.Abp.Demo.EntityFrameworkCore; +// using LINGYUN.Abp.OssManagement.Imaging; namespace PackageName.CompanyName.ProjectName.AIO.Host; @@ -172,12 +150,6 @@ namespace PackageName.CompanyName.ProjectName.AIO.Host; typeof(AbpNotificationsApplicationModule), typeof(AbpNotificationsHttpApiModule), typeof(AbpNotificationsEntityFrameworkCoreModule), - - //typeof(AbpIdentityServerSessionModule), - //typeof(AbpIdentityServerApplicationModule), - //typeof(AbpIdentityServerHttpApiModule), - //typeof(AbpIdentityServerEntityFrameworkCoreModule), - typeof(AbpOpenIddictAspNetCoreModule), typeof(AbpOpenIddictAspNetCoreSessionModule), typeof(AbpOpenIddictApplicationModule), @@ -188,6 +160,9 @@ namespace PackageName.CompanyName.ProjectName.AIO.Host; typeof(AbpOpenIddictWeChatModule), typeof(AbpOpenIddictWeChatWorkModule), + typeof(AbpBackgroundWorkersHangfireModule), + typeof(AbpBackgroundJobsHangfireModule), + //typeof(AbpOssManagementMinioModule), // 取消注释以使用Minio typeof(AbpOssManagementFileSystemModule), // typeof(AbpOssManagementImagingModule), @@ -210,11 +185,6 @@ namespace PackageName.CompanyName.ProjectName.AIO.Host; typeof(AbpSaasHttpApiModule), typeof(AbpSaasEntityFrameworkCoreModule), - typeof(TaskManagementDomainModule), - typeof(TaskManagementApplicationModule), - typeof(TaskManagementHttpApiModule), - typeof(TaskManagementEntityFrameworkCoreModule), - typeof(AbpTextTemplatingDomainModule), typeof(AbpTextTemplatingApplicationModule), typeof(AbpTextTemplatingHttpApiModule), @@ -246,19 +216,7 @@ namespace PackageName.CompanyName.ProjectName.AIO.Host; typeof(AbpPermissionManagementEntityFrameworkCoreModule), typeof(AbpPermissionManagementDomainOrganizationUnitsModule), // 组织机构权限管理 -#if MySQL - typeof(AbpEntityFrameworkCoreMySQLModule), -#elif SqlServer - typeof(AbpEntityFrameworkCoreSqlServerModule), -#elif Sqlite - typeof(AbpEntityFrameworkCoreSqliteModule), -#elif Oracle - typeof(AbpEntityFrameworkCoreOracleModule), -#elif OracleDevart - typeof(AbpEntityFrameworkCoreOracleDevartModule), -#elif PostgreSql typeof(AbpEntityFrameworkCorePostgreSqlModule), -#endif typeof(AbpAliyunSmsModule), typeof(AbpAliyunSettingManagementModule), @@ -268,24 +226,9 @@ namespace PackageName.CompanyName.ProjectName.AIO.Host; typeof(AbpAuthorizationOrganizationUnitsModule), typeof(AbpIdentityOrganizaztionUnitsModule), - typeof(AbpBackgroundTasksModule), - typeof(AbpBackgroundTasksActivitiesModule), - typeof(AbpBackgroundTasksDistributedLockingModule), - typeof(AbpBackgroundTasksEventBusModule), - typeof(AbpBackgroundTasksExceptionHandlingModule), - typeof(AbpBackgroundTasksJobsModule), - typeof(AbpBackgroundTasksNotificationsModule), - typeof(AbpBackgroundTasksQuartzModule), - typeof(AbpDataProtectionManagementApplicationModule), typeof(AbpDataProtectionManagementHttpApiModule), typeof(AbpDataProtectionManagementEntityFrameworkCoreModule), - - // typeof(AbpDemoApplicationModule), - // typeof(AbpDemoHttpApiModule), - // typeof(AbpDemoEntityFrameworkCoreModule), - - typeof(AbpDaprClientModule), typeof(AbpExceptionHandlingModule), typeof(AbpEmailingExceptionHandlingModule), typeof(AbpFeaturesLimitValidationModule), @@ -327,17 +270,6 @@ namespace PackageName.CompanyName.ProjectName.AIO.Host; typeof(AbpAccountTemplatesModule), typeof(AbpAspNetCoreAuthenticationJwtBearerModule), typeof(AbpCachingStackExchangeRedisModule), - // typeof(AbpElsaModule), - // typeof(AbpElsaServerModule), - // typeof(AbpElsaActivitiesModule), - // typeof(AbpElsaEntityFrameworkCoreModule), - // typeof(AbpElsaEntityFrameworkCorePostgreSqlModule), - // typeof(AbpElsaModule), - // typeof(AbpElsaServerModule), - // typeof(AbpElsaActivitiesModule), - // typeof(AbpElsaEntityFrameworkCoreModule), - // typeof(AbpElsaEntityFrameworkCoreMySqlModule), - typeof(AbpExporterMiniExcelModule), typeof(AbpAspNetCoreMvcUiMultiTenancyModule), typeof(AbpAspNetCoreSerilogModule), @@ -348,7 +280,7 @@ namespace PackageName.CompanyName.ProjectName.AIO.Host; typeof(AbpAspNetCoreMvcUiBasicThemeModule), typeof(AbpEventBusModule), typeof(AbpAutofacModule), - + typeof(ProjectNameApplicationModule), typeof(ProjectNameHttpApiModule), typeof(ProjectNameEntityFrameworkCoreModule), @@ -365,7 +297,6 @@ public partial class MicroServiceApplicationsSingleModule : AbpModule PreConfigureFeature(); PreConfigureIdentity(); PreConfigureApp(configuration); - PreConfigureQuartz(configuration); PreConfigureAuthServer(configuration); PreConfigureElsa(context.Services, configuration); PreConfigureCertificate(configuration, hostingEnvironment); @@ -386,7 +317,7 @@ public partial class MicroServiceApplicationsSingleModule : AbpModule ConfigureDataSeeder(); ConfigureLocalization(); ConfigureKestrelServer(); - ConfigureBackgroundTasks(); + ConfigureHangfire(context.Services, configuration); ConfigureExceptionHandling(); ConfigureVirtualFileSystem(); ConfigureEntityDataProtected(); diff --git a/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/PackageName.CompanyName.ProjectName.AIO.Host.csproj b/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/PackageName.CompanyName.ProjectName.AIO.Host.csproj index 9e7ea06d7..f5c2d27ba 100644 --- a/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/PackageName.CompanyName.ProjectName.AIO.Host.csproj +++ b/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/PackageName.CompanyName.ProjectName.AIO.Host.csproj @@ -2,264 +2,260 @@ net9.0 enable + $(NoWarn);CS1591;CS0436;CS8618;NU1803;NU1900 - + - - - - - + + + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/Program.cs b/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/Program.cs index 3daf07fe4..cbbf3048a 100644 --- a/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/Program.cs +++ b/aspnet-core/templates/aio/content/host/PackageName.CompanyName.ProjectName.AIO.Host/Program.cs @@ -76,6 +76,7 @@ app.UseSwaggerUI(options => options.SwaggerEndpoint("/swagger/v1/swagger.json", "Support App API"); }); app.UseAuditing(); +app.UseAbpHangfireDashboard(); app.UseAbpSerilogEnrichers(); app.UseConfiguredEndpoints();