When deploying a release build of my Blazor Server app and hosting it with IIS, the /hangfire
endpoint is caught by the default Blazor routing as “not found”. However, it is working locally, in debug mode, using IIS Express.
How do I get the hangfire dashboard to work on a Blazor Server app in IIS on a server?
Implementation
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHangfire(x =>
{
x.UseSqlServerStorage(builder.Configuration.GetConnectionString("DefaultConnection"), new SqlServerStorageOptions
{
PrepareSchemaIfNecessary = true,
});
x.UseConsole();
});
builder.Services.AddHangfireServer();
var app = builder.Build();
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
Authorization = new[]
{
new HangfireCustomBasicAuthenticationFilter{
User = "test",
Pass = "test"
}
}
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapHangfireDashboard();
endpoints.MapFallbackToPage("/_Host");
});
Just a guess, since I don’t code in Blazor, but this line is probably overwriting your UseHangfireDashboard
settings:
endpoints.MapHangfireDashboard();
Everything else in your configuration seems correct, based on how I have Hangfire configured in an ASP.NET 6 app.
I’m basing this assumption off a very quick glance at the source code for this extension method, for here: https://github.com/HangfireIO/Hangfire/blob/98193fa12d2681661fe852eed72e16eb57653fc0/src/Hangfire.AspNetCore/HangfireEndpointRouteBuilderExtensions.cs#L30
I used only endpoints.MapHangfireDashboard()
. then only app.UseHangfireDashboard()
and vice versa, did not fix the issue.
Setting IgnoreAntiforgeryToken = true
for the dashboard seems to have fixed the issue. Perhaps Blazor Server and Hangfire use conflicting or different Anti Forgery Tokens when enabling security?
- Removed using endpoints
- Added
IgnoreAntiforgeryToken = true
- Removed Authorization for Dashboard as this was also not working
Working implementation Blazor Server
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
IgnoreAntiforgeryToken = true,
});