Hangfire: Distributed Deploy Hangfire client in ASP.NET Core 2.0 thorw 'JobStorage.Current property value has not been initialized.'

Created on 3 Mar 2018  路  5Comments  路  Source: HangfireIO/Hangfire

I just want to add job to Hangfire, and don't trying to complete process.

So, I added the following in ConfigureServices and I remove UseHangfireServer in Configure().

public void ConfigureServices(IServiceCollection services)
{
    services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection")));
}

public void Configure(IApplicationBuilder app)
{
    RecurringJob.AddOrUpdate<IProductManager>(m => m.AutoUpdateAsync(), Cron.Daily(17));
}

And I got error of:

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

Most helpful comment

Use IRecurringJobManager instance from DI container:
c# public void Configure(IApplicationBuilder app, IRecurringJobManager recurringJobManager) { recurringJobManager.AddOrUpdate( "ProductsAutoUpdate", Job.FromExpression<IProductManager>(m => m.AutoUpdateAsync()), Cron.Daily(17)); }
Unfortunately there's currently no generic extension methods for it, so you'll need to add a job in an ugly way.

All 5 comments

If you need a minimal hangfire setup, just to enqueue a job, you could do something like the following

     var jobStorage = new SqlServerStorage(connectionString, new SqlServerStorageOptions {
                PrepareSchemaIfNecessary = false });
     var mgr = new RecurringJobManager(jobStorage);
     var job = Job.FromExpression(m => m.AutoUpdateAsync());
     var id = $"{job.Type.ToGenericTypeString()}.{job.Method.Name}";
     mgr.AddOrUpdate<IProductManager>(id, 
         job Cron.Daily(17), 
        TimeZoneInfo.Utc,,
        EnqueuedState.DefaultQueue);

This does basically what the static extension methods in the RecurringJob class do, but without relying on any state to be set up.

Use IRecurringJobManager instance from DI container:
c# public void Configure(IApplicationBuilder app, IRecurringJobManager recurringJobManager) { recurringJobManager.AddOrUpdate( "ProductsAutoUpdate", Job.FromExpression<IProductManager>(m => m.AutoUpdateAsync()), Cron.Daily(17)); }
Unfortunately there's currently no generic extension methods for it, so you'll need to add a job in an ugly way.

@pieceofsummer Thanks, It's worked.

Just in case for future people finding the same issue

If you want to enqueue jobs using the BackgroundJob or RecurringJob static methods in an application that does not run the Hangfire Server or Dashboard, you can do the following to make them work.

Add to the Configure(IApplicationBuilder app) method of your Startup class:
```C#
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...

// Initialize the Hangfire API so static BackgroundJob and RecurringJob methods can be used
app.ApplicationServices.GetRequiredService<IBackgroundJobClient>();
app.ApplicationServices.GetRequiredService<IRecurringJobManager>();

// ...

}
```

Hi 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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

odinserj picture odinserj  路  4Comments

cbmek picture cbmek  路  3Comments

osmanrahimi picture osmanrahimi  路  3Comments

JvanderStad picture JvanderStad  路  3Comments

dealproc picture dealproc  路  3Comments