C# using Hangfire with Owin and TopShelf

201 Views Asked by At

I created a console application with TopShelf. Now I need to add Hangfire and Owin to activate the Hangfire dashboard. The problem is on my IAppBuilder instance that doesn't have any UseHangfire. I installed Hangfire and Hangfire.AspNetCore but nothing works.

using Hangfire;
using Hangfire.SqlServer;
using Hangfire.Dashboard;
using Owin;

namespace MyNamespace
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseHangfireDashboard(); // <-- Method not found
        }
    }
}

I realy don't undestand where is the problem.

1

There are 1 best solutions below

2
rfcdejong On

I would not use Topshelf anymore, since .NET 3.1 there is a new service available called "BackgroundService"

https://learn.microsoft.com/en-us/dotnet/core/extensions/windows-service?pivots=dotnet-7-0

Else there must be some documentation running Topshelf on .NET 6 or .NET 7

You can create it with something like this (I haven't tested it)

using App.WindowsService;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Logging.EventLog;
using Hangfire;

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddWindowsService(options =>
{
    options.ServiceName = "My Job Service";
});

builder.Services.AddSingleton<MyJobService>();
builder.Services.AddHostedService<WindowsBackgroundService>();

// Add services to the container.
builder.Services.AddHangfire(config =>
{
  config.UseInMemoryStorage();
});
builder.Services.AddHostedService<Worker>();
builder.Services.AddHangfireServer();

var app = builder.Build();
app.UseHangfireDashboard();
app.Run();