如何在控制台应用程序中成功运行 Hangfire MemoryStorage 作业?

Phr*_*nux 5 .net c# console-application hangfire

我正在努力使用 MemoryStorage 在简单的 C# 控制台应用程序上启动 Hangfire 作业。我想用 Hangfire 尝试一些东西,但我就是不知道如何配置它。

这是我的代码:

    private static void Main(string[] args)
    {
        GlobalConfiguration.Configuration.UseMemoryStorage();

        Hangfire.BackgroundJob.Enqueue(() => Console.WriteLine("fire!"));
        Hangfire.RecurringJob.AddOrUpdate(() => Console.WriteLine("minute!"), Cron.Minutely);

        Console.ReadKey();
    }
Run Code Online (Sandbox Code Playgroud)

我没有收到任何这些消息。

我也尝试过使用JobStorage.Current = new MemoryStorage(new MemoryStorageOptions());,但它没有改变任何东西。

Ily*_*kov 4

如果使用内存存储,则必须将 Hangfire Server(即工作线程池)添加到声明存储的同一进程中(存储只是一个ConcurrentDictionary实例)。

在控制台应用程序中,它可能如下所示:

static void Main(string[] args)
{
    GlobalConfiguration.Configuration.UseMemoryStorage();

    BackgroundJob.Enqueue(() => Console.WriteLine("Easy!"));

    using (new BackgroundJobServer())
    {
        Console.WriteLine("Hangfire Server started. Press ENTER to exit...");
        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)