简体   繁体   English

通过控制台应用程序设置作业时如何访问hangfire仪表板

[英]How to access hangfire dashboard when jobs are setup via console application

I have created a .NET Framework 4.5 application using the Hangfire.Core and Hangfire.SqlServe r nuget packages.我使用Hangfire.CoreHangfire.SqlServe r nuget 包创建了 .NET Framework 4.5 应用程序。 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.运行此程序后,我看到在数据库中创建了表模式,并且[Job]表填充了此方法的条目。 However, I don't know how to access the hangfire dashboard to view the job itself.但是,我不知道如何访问 hangfire 仪表板来查看作业本身。 I tried http://localhost/hangfire but that reports a 404 error.我试过 http://localhost/hangfire 但报告了 404 错误。 After some googling, I added following Startup.cs class in the project.经过一番谷歌搜索,我在项目中添加了以下Startup.cs class。

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.我在Configuration方法中设置了一个断点,但它没有被击中,我仍然无法访问 hangfire 仪表板。 Any suggestions what I am doing wrong here?有什么建议我在这里做错了吗?

You created two different classes, one for HangfireSample (needed for the dashboard) and one for the Main (Console).您创建了两个不同的类,一个用于HangfireSample (仪表板需要),一个用于Main (控制台)。 The issue I see is that you dont have HangfireSample called anywhere in your Main class.我看到的问题是您在Main class 的任何地方都没有调用HangfireSample

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) .我创建了一个示例 HangfireProject(带有 Hangfire 1.7.11/MS SQL 的 .Net 框架),并使用WebApp.Start<Dashboard>(options)成功启动并运行了仪表板。 This line actually starts up your Hangfire Dashboard.这一行实际上启动了您的 Hangfire 仪表板。

Dashboard.cs仪表板.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主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.为了能够运行WebApp.Start<> ,您还需要安装Microsoft.Owin.Host.HttpListener Nuget package。 Make sure you are running this project with Admin privileges as it requires access to create the URLs确保您以管理员权限运行此项目,因为它需要访问权限才能创建 URL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM