.Net Core Error on BackgroundJob.Enqueue

Tags: #<Tag:0x00007f23569f4900> #<Tag:0x00007f23569f4838> #<Tag:0x00007f23569f4748>

So when I execute BackgroundJob.Enqueue I get the following Exception for version 1.6.12.

JobStorage.Current property value has not been initialized

My ConfigureServices is as follows my Server is running fine in a standalone Winform app.

services.AddHangfire(v => v.UseSqlServerStorage(configFile.Root.GetConnectionString("HangfireDB")));

If I wire it up as below it seems to work, am I configuring the client incorrectly to receive this error?

services.AddHangfire(configuration => { });
JobStorage.Current = new SqlServerStorage(configFile.Root.GetConnectionString("HangfireDB"));

Winform Hangfire Server

Configuration block is not called in client-only scenarios unless you inject the related service somewhere. You should either explicitly initialize JobStorage.Current (as you did), or use IBackgroundJobClient service instead of static methods (preferred).

1 Like

Thanks I went with injecting the IBackgroundJobClient and just getting the IBackgroundJobClient instance via the MVC Controller constructor. Works perfect.

 services.AddTransient<IBackgroundJobClient>(provider => new BackgroundJobClient(new
                    SqlServerStorage(configFile.Root.GetConnectionString("HangfireDB"))));

You don’t even need to add it to service container, as AddHangfire() does it for you.

Winner winner. :smile: Thanks! I did as you suggested and was able to get the IBackgroundJobClient service from the Controller.

Add this to ConfigureServices

 services.AddHangfire(configuration =>
                {
                    configuration.UseSqlServerStorage(configFile.Root.GetConnectionString("HangfireDB"));
                });

Add this to the MVC Controller
public MyController(IBackgroundJobClient jobClient)