首页 > 解决方案 > 如何在控制台应用程序中配置 hangfire 仪表板?

问题描述

我正在使用 hangfire nuget 包来安排 asp.net 核心控制台应用程序中的作业

我尝试了所有将仪表板配置到控制台应用程序的方法

我如何从控制台应用程序托管网页???

我为仪表板配置创建了 startup.cs 类

using Hangfire;
using Microsoft.AspNetCore.Builder;

    namespace PulsarHangFire
    {
        public class Startup
        {
            public void Configuration(IApplicationBuilder app)
            {
                app.UseHangfireDashboard("/hangfire");
                app.UseHangfireServer();
            }
        }
    }

谁能告诉我我该如何前进

标签: asp.net-coreconsole-applicationhangfire

解决方案


创建一个 Startup.cs 文件(或从 .NET Core Web App 模板中获取一个)并配置以下内容:

public void ConfigureServices(IServiceCollection services)
{
    // ... other required services ...

    services.AddHangfire(configuration =>
    {
        // Do pretty much the same as you'd do with 
        // GlobalConfiguration.Configuration in classic .NET

        // NOTE: logger and activator would be configured automatically, 
        // and in most cases you don't need to configure those.

        configuration.UseSqlServerStorage(...);

        // ... maybe something else, e.g. configuration.UseConsole()
    });
}

最后添加 Hangfire 仪表板:

public void Configure(IApplicationBuilder app, IRecurringJobManager recurringJobManager)
{
    // ... previous pipeline stages, e.g. app.UseAuthentication()

    app.UseHangfireDashboard(...);

   // ... next pipeline stages, e.g. app.UseMvc()

   // then you may configure your recurring jobs here:
   recurringJobManager.AddOrUpdate(...);
}

资源


推荐阅读