Reputation: 305
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.
Upvotes: 3
Views: 5904
Reputation: 4003
In some cases only the app.UseHangfireDashboard();
already do the job(enables Hangfire Dashboard).
.NET 6 Program.cs
using Hangfire;
using Microsoft.AspNetCore.Hosting;
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder =>
{
builder.Configure(app => { app.UseHangfireDashboard(); });
})
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
services.AddHangfire(x => x.UseSqlServerStorage("<connection string>"));
})
.Build();
await host.RunAsync();
Upvotes: 3
Reputation: 47
This example does not work without using Microsoft.AspNetCore.Hosting;
Thanks to LMio from question[ https://stackoverflow.com/questions/72828395/hangfire-server-in-net-worker-service]
Upvotes: 0
Reputation: 1417
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
.
Upvotes: 6