Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Hangfire service in .NET 8 with the new .NET 8 Background Service feature

We have been running Hangfire in a .NET framework 4.7.2 Windows Service together with TopShelf and the Dashboard UI in a separate ASP.NET MVC website. This has been working just fine but now we want to upgrade to .NET 8.

What is the recommended approach considering the new built in BackgroundService of .NET Core? I assume that an ordinary Windows Service is no longer required?

like image 890
Mr W Avatar asked Nov 15 '25 22:11

Mr W


1 Answers

Hangfire has now new package Hangfire.AspNetCore core which has full support of Microsoft DI. To set it up as part of the Windows service in .NET8 :

var host = hostbuilder.UseWindowsService()
.ConfigureServices((hostContext, services) => {
  
        // Add Hangfire services.
        services.AddHangfire(configuration => configuration
            .UseSimpleAssemblyNameTypeSerializer()
            .UseRecommendedSerializerSettings()
            .UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection")));
    
        // Add the processing server as IHostedService
        services.AddHangfireServer();
    
    })

No need anymore for custom JobActivator cuz there is built in Activator with Microsoft DI which creates a scope a resolves all job dependencies.

For your question do you keep it still as windows service - in my mind for Hangfire server big YES, dashboard can still be on API.

But you can move it to be part of your API as well.

We have moved to .NET8 and our Hangfire server is still windows service and working like a charm.

If you are far behind with your Hangfire schema , right now its on 9 - if you wanna migrate to latest hangfire SQL schema - enable heavy migrations. Guide to migration.

like image 64
J.Memisevic Avatar answered Nov 17 '25 20:11

J.Memisevic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!