SoFunction
Updated on 2025-04-05

Abp integrates HangFire open source .NET task scheduling framework

Brief description

Backend jobs are a relatively common function in the process of system development. Because there are always some long and time-consuming tasks that we do not respond immediately, such as Excel document import, batch sending SMS notifications, etc.

ABP vNext provides support for background jobs and also provides background job integration for HangFire and RabbitMQ. When developers use these third-party libraries, they basically use them out of the box without making other complicated configurations.

The implementation of ABP vNext is to encapsulate a layer on the Timer of CLR and execute user logic periodically.
The background task manager provided by ABP vNext by default is encapsulated based on background jobs.

There are 6 modules involving background tasks, and they are:

  • : Provides some commonly used thread components, among which AbpTimer is implemented.
  • : Definition and implementation of background tasks.
  • : Some common definitions of background tasks.
  • : The default background task manager implementation.
  • : A background task manager based on the Hangfire library.
  • : A background task manager based on RabbitMQ implementation.

What is Hangfire

Hangfire is an open source .NET task scheduling framework, currently .NET Core is supported on version 1.6+. I personally think that its biggest feature is that it provides an integrated console built-in, which is convenient for background viewing and monitoring.

Quote

<PackageReference Include="" Version="1.0.2" />
<PackageReference Include="" Version="1.0.2" />
<PackageReference Include="Hangfire" Version="1.7.7" />
<PackageReference Include="" Version="1.7.7" />
<PackageReference Include="" Version="1.0.2" />
<PackageReference Include="" Version="1.7.0" />

start up

public class Startup
   {
       public void ConfigureServices(IServiceCollection services)
       {
           ();
           var Configuration = ();
           (new SucceededStateExpireHandler((Configuration["Hangfire:JobExpirationTimeout"])));
           ();
           (x =&gt;
           {
               var connectionString = Configuration["Hangfire:Redis:ConnectionString"];
               (connectionString, new RedisStorageOptions()
               {
                   //Active server timeout time                   InvisibilityTimeout = (60),
                   Db = (Configuration["Hangfire:Redis:Db"])
               });
               ()
              .UseDashboardMetric()
              .UseDashboardMetric()
              .UseDashboardMetric()
              .UseDashboardMetric()
              .UseDashboardMetric()
              .UseDashboardMetric()
              .UseDashboardMetric()
              .UseDashboardMetric()
              .UseDashboardMetric()
              .UseDashboardMetric()
              .UseDashboardMetric();
           });
       }
       public void Configure(IApplicationBuilder app, IConfiguration Configuration)
       {
           ();
           ();
           var filter = new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions
       {
           SslRedirect = false,
           RequireSsl = false,
           LoginCaseSensitive = false,
           Users = new[]
           {
                       new BasicAuthAuthorizationUser
                       {
                           Login = Configuration["Hangfire:Login"] ,
                           PasswordClear= Configuration["Hangfire:PasswordClear"]
                       }
           }
       });
           ("", new DashboardOptions
           {
               Authorization = new[]
               {
                  filter
               },
           });
           var jobOptions = new BackgroundJobServerOptions
           {
               Queues = new[] { "critical", "test", "default" },
               WorkerCount =  * (Configuration["Hangfire:ProcessorCount"]),
               ServerName = Configuration["Hangfire:ServerName"],
               SchedulePollingInterval = (1), //Scheduled polling interval Support task to seconds           };
           (jobOptions);
       }
   }

set up

///
   /// Completed job settings expire to prevent unlimited data growth   ///
   public class SucceededStateExpireHandler : IStateHandler
   {
       public TimeSpan JobExpirationTimeout;
       public SucceededStateExpireHandler(int jobExpirationTimeout)
       {
           JobExpirationTimeout = (jobExpirationTimeout);
       }
       public string StateName =&gt; ;
       public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction)
       {
            = JobExpirationTimeout;
       }
       public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction)
       {
       }
   }

The above is the detailed content of Abp integrated HangFire open source .NET task scheduling framework. For more information about Abp integrated HangFire framework, please follow my other related articles!