JobStorage.Current property value has not been ini

2019-08-23 04:50发布

问题:

I am using hangfire in mvc application. I am sending reminder to user for his/her appointment. I have installed hangfire in my app. I have configured hangfire in startup.cs class. But when i run the app, it produce the below error, JobStorage. Current property value has not been initialized. You must set it before using Hangfire Client or 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();

        }
    }
}

回答1:

For Initializing in Asp.net core

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


回答2:

There is same question in this link. I hope this helps you.

Can you write code which throws exception? I write your Startup class and test controller -below-. It works fine. I did not faced any exception.

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