Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JobStorage.Current property value has not been initialized. You must set it before using Hangfire Client or Server API

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

        }
    }
}
like image 718
Ali Avatar asked Aug 31 '18 10:08

Ali


4 Answers

you can use this road:

1-Installing Hangfire->Hangfire.AspNetCore(v1.7.14) and Hangfire.Core(v1.7.14)

2-Registering Services

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- Adding Dashboard UI

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- Running Application The following message should also appear, since we created background job, whose only behavior is to write a message to the console. Hello world from Hangfire!

like image 134
elnaz nasiri Avatar answered Nov 09 '22 01:11

elnaz nasiri


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;
        }
like image 11
Qamar Zaman Avatar answered Nov 09 '22 01:11

Qamar Zaman


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());
    }
}
like image 2
Adem Catamak Avatar answered Nov 09 '22 01:11

Adem Catamak


With the version 1.7.30 if I initialize Hangfire like this:

services.AddHangfire(c => c.UseSqlServerStorage(configuration.GetConnectionString("Default")));
services.AddHangfireServer();

and then try to call, for example, RecurringJob.AddOrUpdate in the Configure method, I will have exception from the question title. I didn't have it locally, because locally I have development environment, and applicationBuilder.UseHangfireDashboard() is called. If this method is called before, RecurringJob.AddOrUpdate will work without any problem.

Another way is to inject the IBackgroundJobClient (or get it from the IServiceProvider, it is not important) before calling RecurringJob.AddOrUpdate. You don't need to do anything with the IBackgroundJobClient instance, just inject it. After that everything works. I hope it will save time for someone.

like image 1
Dmitry Sikorsky Avatar answered Nov 09 '22 01:11

Dmitry Sikorsky