asp.net-corehangfire.net-8.0

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?


Solution

  • 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.