I am using Hangfire to schedule jobs in my worker service and want to use the hangfire dashboard. But it seems that there is no way to configure this. All the documentation uses the Startup class but I don't have any startup in my worker service. Also, the OWIN NuGet package is not supported in .Net 5. Here is what I've tried,
var hostBuilder = CreateHostBuilder(args)
.Build();
var services = hostBuilder.Services;
var applicationBuilder = new ApplicationBuilder(services);
applicationBuilder.UseRouting();
applicationBuilder.UseHangfireDashboard("/hangfire");
applicationBuilder.UseEndpoints(endpoints =>
{
endpoints.MapHangfireDashboard();
});
hostBuilder.Run();
and I've configured hangfire like this,
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage("connection string",
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true
}));
// Add the processing server as IHostedService
services.AddHangfireServer();
Please note that I am able to schedule and execute jobs by hangfire in the current implementation, all I need now is to configure the hangfire dashboard.
Use the following Program.cs
to configure Hangfire dashboard and your worker service:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args)
.Build()
.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder =>
{
builder.Configure(app =>
{
app.UseRouting();
app.UseHangfireDashboard();
app.UseEndpoints(endpoints =>
{
endpoints.MapHangfireDashboard();
});
});
})
.ConfigureServices((hostContext, services) =>
{
services.AddHangfire(conf => conf.UseSqlServerStorage("connection string"));
services.AddHangfireServer();
// your worker service
services.AddHostedService<Worker>();
});
}
Hangfire dashboard will be available at http://localhost:5000/hangfire
.