Reputation: 35
I have created a .NET Framework 4.5 application using the Hangfire.Core
and Hangfire.SqlServe
r nuget packages. The code is pretty straight forward.
class Program
{
static void Main(string[] args)
{
GlobalConfiguration.Configuration
.UseSqlServerStorage("Server=localhost;Database=HangfireDb;User Id=username;Password=password");
BackgroundJob.Enqueue(() => ProcessData("process this"));
using (var server = new BackgroundJobServer())
{
Console.WriteLine("Hangfire Server started. Press any key to exit...");
Console.ReadKey();
}
}
public static void ProcessData(string data)
{
Console.WriteLine(data);
}
}
After running this program, I see the table schema is created in the database, and the [Job]
table gets populated with an entry for this method. However, I don't know how to access the hangfire dashboard to view the job itself. I tried http://localhost/hangfire but that reports a 404 error. After some googling, I added following Startup.cs
class in the project.
using Hangfire;
using Microsoft.Owin;
using Owin;
using System.Collections.Generic;
[assembly: OwinStartup(typeof(HangfireSample.Startup))]
namespace HangfireSample
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseHangfireServer();
app.UseHangfireDashboard("/hangfire");
}
}
}
I set up a break-point in the Configuration
method but it didn't get hit and I still cannot access the hangfire dashboard. Any suggestions what I am doing wrong here?
Upvotes: 2
Views: 4116
Reputation: 11364
You created two different classes, one for HangfireSample
(needed for the dashboard) and one for the Main
(Console). The issue I see is that you dont have HangfireSample
called anywhere in your Main
class.
I created a sample HangfireProject (.Net Framework with Hangfire 1.7.11/MS SQL) and successfully got the dashboard up and running with the use of the WebApp.Start<Dashboard>(options)
. This line actually starts up your Hangfire Dashboard.
Dashboard.cs
using Hangfire;
using HangfireProject;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(Dashboard))]
namespace HangfireProject
{
public class Dashboard
{
public void Configuration(IAppBuilder app)
{
//app.UseHangfireServer();
app.UseHangfireDashboard();
}
}
}
Main Class
using Hangfire;
using Hangfire.SqlServer;
using Microsoft.Owin.Hosting;
using System;
using System.Collections.Generic;
namespace HangfireProject
{
class Program
{
static void Main(string[] args)
{
GlobalConfiguration.Configuration
.UseSqlServerStorage("Server=localhost;Database=HangfireDB;User Id=localAccount;Password=password;", new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true
});
StartOptions options = new StartOptions();
options.Urls.Add($"http://localhost:9095");
options.Urls.Add($"http://127.0.0.1:9095");
WebApp.Start<Dashboard>(options);
using (var server = new BackgroundJobServer())
{
System.Console.WriteLine("Hangfire Server started. Press any key to exit...");
System.Console.ReadKey();
}
}
}
}
For you to be able to run the WebApp.Start<>
, you will also need to install the Microsoft.Owin.Host.HttpListener
Nuget package. Make sure you are running this project with Admin privileges as it requires access to create the URLs
Upvotes: 2