我在mvc应用程序中使用hangfire。我正在向用户发送提醒他/她的约会。我在我的应用中安装了hangfire。我在startup.cs类中配置了hangfire。但是当我运行应用程序时,会产生以下错误,JobStorage。当前属性值尚未初始化。您必须在使用Hangfire客户端或服务器API之前进行设置。
using Hangfire;
using Hangfire.SqlServer;
using Microsoft.Owin;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using UKC.Data.Infrastructure;
using UKC.UI.Helper;
[assembly: OwinStartup(typeof(UKC.UI.App_Start.Startup))]
namespace UKC.UI.App_Start
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
GlobalConfiguration.Configuration
.UseSqlServerStorage("DbEntities");
app.UseHangfireDashboard();
app.UseHangfireServer();
}
}
}
public static void InitializeHangFire()
{
var sqlStorage = new SqlServerStorage("connectionString");
var options = new BackgroundJobServerOptions
{
ServerName = "Test Server"
};
JobStorage.Current = sqlStorage;
}
在this link有同样的问题。我希望这可以帮助你。
你能编写引发异常的代码吗?我写你的Startup
类和测试控制器-below-。它工作正常。我没有遇到任何例外。
[RoutePrefix("")]
public class HomeController : ApiController
{
[Route(""), HttpGet]
public void Get()
{
Hangfire.BackgroundJob.Enqueue(() => Tasks.DoIt("test"));
Hangfire.BackgroundJob.Schedule(() => Tasks.InitializeJobs(), TimeSpan.FromSeconds(5));
}
}
public static class Tasks
{
public static void DoIt(string s)
{
Console.WriteLine(s);
}
public static void InitializeJobs()
{
Console.WriteLine(DateTime.Now.ToString());
}
}