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

How to successfully run Hangfire MemoryStorage jobs in console application?

我正在努力使用 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();
    }

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

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

如果使用内存存储,您必须将 Hangfire 服务器(即工作线程池)添加到声明存储的同一进程(存储只是一个 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();
    }
}