Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an in memory job storage package for Hangfire?

Tags:

c#

hangfire

I have a console application to test HangFire. Here is the code:

using System; using Hangfire;  namespace MyScheduler.ConsoleApp {     internal static class Program     {         internal static void Main(string[] args)         {             MyMethod();              Console.WriteLine("[Finished]");             Console.ReadKey();         }          private static void MyMethod()         {             RecurringJob.AddOrUpdate(() => Console.Write("Easy!"), Cron.Minutely);         }     } } 

But it throws an exception on runtime:

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

So I need a job storage to run this. But all examples in SQL storage etc. Is there any way to run this example with some kind of memory storage?

JobStorage.Current = new SqlServerStorage("ConnectionStringName", options);   // to   JobStorage.Current = new MemoryDbStorage(string.Empty, options);   
like image 240
Lost_In_Library Avatar asked Apr 04 '17 12:04

Lost_In_Library


People also ask

What is hangfire .NET core?

The Hangfire. AspNetCore integration package adds an extension method to register all the services, their implementation, as well as logging and a job activator. As a parameter, it takes an action that allows to configure Hangfire itself.


2 Answers

You can use Hangfire.MemoryStorage for this.

Simply add this nuget package.

And then you can use it like -

GlobalConfiguration.Configuration.UseMemoryStorage(); 
like image 70
Yogi Avatar answered Oct 02 '22 12:10

Yogi


For NET Core (web application):

Just to make it very obvious because it wasn't obvious to me.

Install following nuget packages:

  • Hangfire.AspNetCore (v1.6.17 atow)
  • Hangfire.MemoryStorage.Core (v1.4.0 atow)

In Startup.cs:

    public void ConfigureServices(IServiceCollection services)     {         // other registered services         ...          services.AddHangfire(c => c.UseMemoryStorage());     }      public void Configure(IApplicationBuilder app, IHostingEnvironment env)     {         // other pipeline configuration                     ...          app.UseHangfireServer();          app.UseMvc();     } 

Anything less than above and my enqueued method did not fire.

like image 23
Quinton Smith Avatar answered Oct 02 '22 10:10

Quinton Smith