Base solution for your next web application
Open Closed

Hangfire RecurringJob throws exception when called in PostInitialize #7281


User avatar
0
miroslav.engi created

Hi,

In WebHostModule in Postinitialize I call:

RecurringJob.AddOrUpdate<ILabelAppService>("generateLabelInfo", t => t.GenerateLabelInfo(), Cron.Daily);

and I get Exception like Hangfire is not initialized.

System.InvalidOperationException
  HResult=0x80131509
**  Message=JobStorage.Current property value has not been initialized. You must set it before using Hangfire Client or Server API.**
  Source=Hangfire.Core
  StackTrace:
   at Hangfire.JobStorage.get_Current()
   at Hangfire.RecurringJob.<>c.<.cctor>b__20_0()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at Hangfire.RecurringJob.AddOrUpdate(Expression`1 methodCall, String cronExpression, TimeZoneInfo timeZone, String queue)
   at PacapimePortal.Web.Startup.PacapimePortalWebHostModule.PostInitialize() in C:\Projects\PacapimePortal\aspnet-core\src\PacapimePortal.Web.Host\Startup\PacapimePortalWebHostModule.cs:line 72
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at Abp.AbpBootstrapper.Initialize()

I enabled:

//Uncomment this line to use Hangfire instead of default background job manager (remember also to uncomment related lines in Startup.cs file(s)).
Configuration.BackgroundJobs.UseHangfire();

in WebCoreModule.cs

Also in WebConsts public static bool HangfireDashboardEnabled = true;

P.S.

When I put in PostInitialize timeout like:

    var timeToWait = 3000; //ms
    Task.Run(async () =>
    {
        await Task.Delay(timeToWait);
        RecurringJob.AddOrUpdate<ILabelAppService>("generateLabelInfo", t => t.GenerateLabelInfo(), Cron.Daily);
    });

than it works but this is not a solution.

Am I missing something ?

Using version 6.9 of Asp.Zero.


2 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    Related: https://github.com/aspnetzero/aspnet-zero-core/issues/795

  • User Avatar
    0
    miroslav.engi created

    Works thanks.