This is based only on my own brief testing, and is not intended to be an official guide.
I’m using Visual Studio 2022, which installs .NET 6, and I used the “ASP.NET Core Web App” project type/template. I am more familiar with “Web Forms (.NET Framework v4.8) ASP.NET Web” applications, and I am very new to .NET Core applications and even more so to .NET 6. Please reply with any corrections or changes that might be needed or recommended.
Also, this information is only intended to help with the initial setup of Hangfire in an “ASP.NET Core Web App” project, not with any of the code for adding or managing scheduled or recurring tasks.
I started with the documentation on this page for getting started with Hangfire in ASP.NET Core Applications:
ASP.NET Core Applications — Hangfire Documentation
Since this documentation is based on older versions of ASP.NET Core, I also then used this page on migrating [from ASP.NET Core v5] “to the new minimal hosting model in ASP.NET Core 6.0”:
Code samples migrated to the new minimal hosting model in 6.0 | Microsoft Docs
Note: You may need to disable the “Hot reload” features in the “ASP.NET Core” options in Visual Studio 2022 to allow the Hangfire page to load completely - see this discussion topic:
Dashboard page blank after upgrade to .net 6.0
I needed to disable these options to initially have the project page load completely, but later I was able to re-enable these features and the page continued working correctly.
In Visual Studio 2022, I created a new project and selected the following project template:
“ASP.NET Core Web App: A project template for creating an ASP.NET Core application with example ASP.NET Razor Pages content.”
I named the new project, and then selected “.NET 6.0 (Long-term support)” for the framework, and “Configure for HTTPS”, and created the new application. The project should run at this point, and display a simple home page.
Next, run the NuGet Package Manager (right-click on the project name and select “Manage NuGet Packages…”); click the “Browse” tab. Search for, and install, each of the following 3 packages:
Hangfire.AspNetCore
Hangfire.Core
Hangfire.SqlServer
NOTE: I’m connecting to a SQL Server database for Hangfire, so I need the “Hangfire.SqlServer” package; you may or may not need this package.
Create a new empty SQL Server database (if that’s what you’re using for storage) - I created a database named “HangfireTest” in the “LocalDB” instance, which is “(localdb)\MSSQLLocalDB”.
Open the files “appsettings.json” and “appsettings.Development.json” and add the following to the beginning of both files (immediately after the opening “{”):
“ConnectionStrings”: {
“HangfireConnection”: “Server=(localdb)\MSSQLLocalDB;Database=HangfireTest;Integrated Security=SSPI;”
},
Change the connection string as appropriate for your database.
Open the “Program.cs” file; note that there is NO Startup.cs file in this minimal style of .NET 6 Core web application.
At the very beginning of Program.cs, add these 2 lines:
using Hangfire;
using Hangfire.SqlServer;
Immediately AFTER the line “var builder = WebApplication.CreateBuilder(args);” in Program.cs, add the following:
builder.Services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(builder.Configuration.GetConnectionString(“HangfireConnection”), new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true
}));
builder.Services.AddHangfireServer();
I don’t believe that you need to add “builder.Services.AddMvc();” in this project type, since it is using Razor pages and already contains the following: “builder.Services.AddRazorPages();”
Immediately AFTER the line “app.UseAuthorization();” in Program.cs, add:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHangfireDashboard();
});
After the line “app.Run();” in Program.cs, add:
BackgroundJob.Enqueue(() => Console.WriteLine(“Hello, world!”));
using (var server = new BackgroundJobServer())
{
Console.ReadLine();
}
Note: This is probably not the appropriate place to include code to submit jobs to the queue, but this is okay for a test.
Now, you should be able to build and run the application. You may need to accept a self-signed server certificate if you haven’t previously run a .NET 6 project. After that, the tables should be created in the database, and the default/home page for the new app should be displayed in a browser. Once the home page is displayed, you can add “/hangfire” to the end of your local URL, and you should then see the Hangfire dashboard.
You may also want to change the “run” option in Visual Studio (next to the green “run” icon) from the project name to “IIS Express” (so that it doesn’t run a console window).
Does anyone have additional suggestions for this, or are there any errors in the above?