我有一个控制台应用程序来测试HangFire。这是代码:
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);
}
}
}
但是它会在运行时引发异常:
其他信息:JobStorage.Current属性值尚未输入 初始化。您必须在使用Hangfire客户端或服务器之前进行设置 API。
所以我需要一个工作存储来运行它。但是SQL存储中的所有示例等。有没有办法用某种内存存储来运行这个例子?
JobStorage.Current = new SqlServerStorage("ConnectionStringName", options);
// to
JobStorage.Current = new MemoryDbStorage(string.Empty, options);
答案 0 :(得分:36)
您可以使用Hangfire.MemoryStorage。
只需添加this nuget package。
然后你可以像 -
一样使用它GlobalConfiguration.Configuration.UseMemoryStorage();
答案 1 :(得分:14)
对于NET Core(Web应用程序):
只是为了让它变得非常明显,因为这对我来说并不明显。
安装以下nuget包:
在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();
}
任何小于上述的东西,我的排队方法都没有开火。
答案 2 :(得分:4)
为了完整起见,Hangfire 库的作者添加了一个名为 Hangfire.InMemory
的新包,其版本可在 Nuget 上获得。存储库自述文件将其定位为针对生产用途。引用 github repo URL 如下:“.. 一种高效的 Hangfire 事务内存存储,其数据结构接近其最佳表示。这种尝试的结果应该使这种存储实现的生产就绪使用成为可能,并处理特定的属性内存处理..."
熟悉的配置概念在这里也适用:
GlobalConfiguration.Configuration.UseInMemoryStorage();
我个人添加如下:
services.AddHangfire(configuration => { configuration.UseInMemoryStorage(); });
答案 3 :(得分:2)
正如Yogi所说,你可以使用Hangfire.MemoryStorage或Hangfire.MemoryStorage.Core(适用于.Net Core)。
答案中缺少的是需要放在ConfigureServices(..)中的完整代码(对于.Net Core):
var inMemory = GlobalConfiguration.Configuration.UseMemoryStorage();
services.AddHangfire(x => x.UseStorage(inMemory));