Hangfire dashboard not displaying in a console application

64 views Asked by At

So I have Hangfire inside a console app that runs a background job simultaneously. I wanted to check the dashboard on the hangfire but it never appeared to me. Hangfire is executing well.

builder.ConfigureServices(services =>
{

.....

services.AddHangfire((provider, configuration) =>
{
    var dbOptions = provider.GetRequiredService<IOptions<DatabaseOptions>>().Value;
    configuration.UsePostgreSqlStorage(dbOptions.Postgres.ConnectionString);
});

services.AddHangfireServer();

}).ConfigureWebHostDefaults(builder =>
{
    builder.Configure(app =>
    {
        app.UseRouting();
        app.UseMvc();

        app.UseHangfireDashboard("/hangfire", new
            DashboardOptions
            {
                Authorization = new[] { new AuthorizationFilter() }
            });
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapHangfireDashboard();

        });
    });
});


var host = builder.Build();

await host.RunAsync();

I tried to access localhost:5000/hangire but no luck. My Authorization filter is this one:

public class AuthorizationFilter : IDashboardAuthorizationFilter
{
    public bool Authorize(DashboardContext context)
    {
        return true;
      
    }
}

Does anyone have any hints?

1

There are 1 answers

4
J.Memisevic On

I have figure it out for .net8.

UPDATE : Same example works for .net6

First in your csproj add this :

<ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

Then in my Program.cs :

var builder = Microsoft.AspNetCore.Builder.WebApplication.CreateBuilder(new WebApplicationOptions() { ApplicationName = "ConsoleApp1", EnvironmentName = "Development" });
builder.Host.ConfigureServices(
    (context,services) =>
    {
        services.AddControllers();
        services.AddHangfire((provider, configuration) =>
        {
            var dbOptions = "connection_string";
            configuration.UseSqlServerStorage(dbOptions);
        });

        services.AddHangfireServer();
    }
);
var app = builder.Build();


app.UseHangfireDashboard("/hangfire", new
            DashboardOptions
{
    Authorization = new[] { new AuthorizationFilter() }
});

// Configure the HTTP request pipeline.
app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

await app.RunAsync();

I thing that you were missing just include of Microsoft.AspNetCore.App.

NOTE : I'm using SQL Server as persistance because I have already hangfire database ready.

NOTE 2: Maybe it would be easier to have hangfire server configured as part of .NET SDK Worker and Dashboard UI inside the WebAPI.. Not sure what your use case is.