Hangfire setup for different versions (.NET Core 3.1)

Viewed 1193

I'm trying to setup hangfire, but since there has been a few version throughout the history and different setups for ASP.NET and ASP.NET Core applications I'm not sure which one to use.

In Startup.cs in ConfigureServices I use AddHangfire() and AddHangfireServer() extension methods, but I can see there is also UseHangfireServer() on IApplicationBuilder in Configure method. Currently I'm using only AddHangfireServer(), but I'm not sure if I need the other one?

Same thing is for UseHangfireDashboard() on IApplicationBuilder and MapHangfireDashboard() on IEndpointRouteBuilder within UseEndpoints(). Currently I'm using only MapHangfireDashboard().

Though everything seems to work, I'm not sure if everything will behave correctly along the way (I'm not gonna test the whole hangfire just to make sure...), so do I need to add AddHangfireServer() and UseHangfireDashboard(), especially since some example have them while others don't?

2 Answers

You could check the Hangfire document, AddHangfire allows you to configure everything, allowing you to enqueue and run jobs from the app you call it from. AddHangfireServer registers just the bits that run jobs in the background, so you might use this when you create a headless “job execution service” as it is focused solely on running jobs rather than getting input/parameters from the user.

In our app, each server calls AddHangfire but only starts a Hangfire server if configured to do so. Each web server registers a BackgroundJobClient so it can submit jobs to queues without executing them. We designate one server to run jobs, so that server calls UseHangfireServer during its Configure call.

Same thing is for UseHangfireDashboard() on IApplicationBuilder and MapHangfireDashboard() on IEndpointRouteBuilder within UseEndpoints(). Currently I'm using only MapHangfireDashboard().

Besides, about the Dashboard UI, after registering Hangfire types, you can now choose features you need to add to your application. It is fully optional, and you can remove them completely, if your application will only create background jobs, as separate application will process them. And, when using RequireAuthorization with MapHangfireDashboard, be cautious that only local access is allowed by default.

Nowadays as I understand it, you do it like this for ASP.NET Core:

In Startup.ConfigureServices():

// Configure hangfire but don't start anything: 
services.AddHangfire(configuration => configuration
    .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
    .UseSimpleAssemblyNameTypeSerializer()
    .UseRecommendedSerializerSettings()
    .UseSqlServerStorage( 
         Configuration.GetConnectionString("HangfireConnection"), 
         new SqlServerStorageOptions
        {
            CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
            SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
            QueuePollInterval = TimeSpan.Zero,
            UseRecommendedIsolationLevel = true,
            DisableGlobalLocks = true
        }));


// Tell asp.net to start a HostedService to process jobs: 
services.AddHangfireServer();

In Startup.Configure():

// Run the hangfire dashboard at route '/hangfire'
app.UseHangfireDashboard();

Nothing else is needed.

Related