JobStorage.Current 属性值尚未初始化。您必须在使用 Hangfire Client 或 Server API 之前设置它

Ali*_*Ali 9 c# asp.net-mvc hangfire

我在 mvc 应用程序中使用 hangfire。我正在向用户发送预约提醒。我已经在我的应用程序中安装了 hangfire。我已经在 startup.cs 类中配置了 hangfire。但是当我运行该应用程序时,它会产生以下错误 JobStorage。当前属性值尚未初始化。您必须在使用 Hangfire Client 或 Server API 之前设置它。

using Hangfire;
using Hangfire.SqlServer;
using Microsoft.Owin;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using UKC.Data.Infrastructure;
using UKC.UI.Helper;

[assembly: OwinStartup(typeof(UKC.UI.App_Start.Startup))]
namespace UKC.UI.App_Start
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
            GlobalConfiguration.Configuration
               .UseSqlServerStorage("DbEntities");

            app.UseHangfireDashboard();
            app.UseHangfireServer();

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 7

你可以使用这条路:

1-安装Hangfire ->Hangfire.AspNetCore(v1.7.14) 和 Hangfire.Core(v1.7.14)

2-注册服务

class Program
{
    static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args)
    {
      return WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
    }
 }

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Add Hangfire services.
        services.AddHangfire(configuration => configuration
            .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
            .UseSimpleAssemblyNameTypeSerializer()
            .UseRecommendedSerializerSettings()
            .UseSqlServerStorage("Server=-; Database=-; user=-; password=-;"));

        // Add the processing server as IHostedService
        services.AddHangfireServer();
     }
Run Code Online (Sandbox Code Playgroud)

3-添加仪表板用户界面

public void Configure(IApplicationBuilder app, IBackgroundJobClient 
                      backgroundJobs, IHostingEnvironment env)
    {
        app.UseHangfireDashboard();
        backgroundJobs.Enqueue(() => Console.WriteLine("Hello world from 
        Hangfire!"));

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

4- 运行应用程序 由于我们创建了后台作业,因此还应该出现以下消息,该作业的唯一行为是将消息写入控制台。 来自 Hangfire 的世界,你好!


Qam*_*man 6

用于在 Asp.net 核心中初始化

public static void InitializeHangFire()
        {
            var sqlStorage = new SqlServerStorage("connectionString");
            var options = new BackgroundJobServerOptions
            {
                ServerName = "Test Server"
            };
            JobStorage.Current = sqlStorage;
        }
Run Code Online (Sandbox Code Playgroud)


Dmi*_*sky 5

对于 1.7.30 版本,如果我像这样初始化 Hangfire:

services.AddHangfire(c => c.UseSqlServerStorage(configuration.GetConnectionString("Default")));
services.AddHangfireServer();
Run Code Online (Sandbox Code Playgroud)

然后尝试调用,例如,RecurringJob.AddOrUpdateConfigure方法中,我将从问题标题中得到异常。我本地没有,因为本地我有开发环境,并且applicationBuilder.UseHangfireDashboard()被调用。如果之前调用此方法,RecurringJob.AddOrUpdate将没有任何问题。

另一种方法是在调用之前注入IBackgroundJobClient(或从 IServiceProvider 获取,这并不重要)RecurringJob.AddOrUpdate。您不需要对IBackgroundJobClient实例执行任何操作,只需注入它即可。之后一切正常。我希望它能为某人节省时间。

  • 我正在使用 .NET 6,这个答案对我有帮助。事实上,在调用“BackgroundJob.Enqueue”或“RecurringJob.AddOrUpdate”等任何hangfire操作之前,有必要调用“services.AddHangfireServer()”或“app.UseHangfireDashboard()”。然而,就我而言,我不想使用任何一种方法,因为出于架构原因,我需要将 Hangfire 服务器保留在不同的 kubernetes pod(控制台应用程序)中,与我的 WebApi 分开。因此,将“IBackgroundJobClient”注入到 WebAPI 中是更优雅的选择,并且可以完美地工作而不会发布错误。 (2认同)

Ade*_*mak 2

此链接中有同样的问题。我希望这可以帮助你。

你能编写抛出异常的代码吗?我Startup在下面编写了您的类和测试控制器。效果很好。我没有遇到任何例外。

[RoutePrefix("")]
public class HomeController : ApiController
{
    [Route(""), HttpGet]
    public void Get()
    {
        Hangfire.BackgroundJob.Enqueue(() => Tasks.DoIt("test"));

        Hangfire.BackgroundJob.Schedule(() => Tasks.InitializeJobs(), TimeSpan.FromSeconds(5));
    }
}

public static class Tasks
{
    public static void DoIt(string s)
    {
        Console.WriteLine(s);
    }

    public static void InitializeJobs()
    {
        Console.WriteLine(DateTime.Now.ToString());
    }
}
Run Code Online (Sandbox Code Playgroud)