JobStorage.Current属性值尚未初始化。您必须先设置它,然后才能使用Hangfire客户端或服务器API

时间:2018-08-31 10:20:14

标签: c# asp.net-mvc hangfire

我在MVC应用程序中使用hangfire。我正在向用户发送提醒 约定。我已经在我的应用程序中安装了hangfire。我已经在配置了hangfire startup.cs类。但是当我运行该应用程序时,它会产生以下错误, JobStorage。当前属性值尚未初始化。您必须先设置它,然后才能使用Hangfire客户端或服务器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();

        }
    }
}

3 个答案:

答案 0 :(得分:1)

您可以使用这条路:

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();
     }

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();
        }

    }
}

4-运行应用程序 由于我们创建了后台作业,因此以下消息也应出现,其唯一行为是将消息写入控制台。 Hangfire的Hello world!

答案 1 :(得分:0)

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

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

答案 2 :(得分:-1)

this link中存在相同的问题。希望对您有帮助。

您可以编写引发异常的代码吗?我在下面编写您的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());
    }
}