安装后未创建hangfire数据库

时间:2019-03-05 16:09:40

标签: hangfire

我用Nuget安装了篝火。

PM> Install-Package Hangfire

然后用以下几行更新OWIN Startup类:

public void Configuration(IAppBuilder app)
{
    GlobalConfiguration.Configuration.UseSqlServerStorage("MySqlConnection");

    app.UseHangfireDashboard();
    app.UseHangfireServer();
}

在UseSqlServerStorage中,我在web.config上将连接字符串名称命名为connectionString的名称。

web.config:

<connectionStrings>
    <add name="MyEntities" connectionString="metadata=res://*/CRMModelContext.csdl|res://*/CRMModelContext.ssdl|res://*/CRMModelContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=xxx;persist security info=True;user id=sa;password=123;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="MySqlConnection" connectionString="User ID=sa;Initial Catalog=xxx;Password=123;Integrated Security=False;Data Source=." />

SQL Server 2008 R2已安装在系统上,但没有用于进行hangfire的数据库。

最后,当我运行程序时,出现以下错误。

我的工作:

BackgroundJob.Enqueue<Receiver>(x=>x.EmailReceiving());
RecurringJob.AddOrUpdate<Receiver>(x => x.EmailReceiving(), Cron.MinuteInterval(15));

错误:

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

2 个答案:

答案 0 :(得分:0)

GlobalConfiguration.Configuration.UseSqlServerStorage("MySqlConnection");使用MySqlConnection作为连接字符串,而不是MySqlConnection的值!

您使用的是MsSql Server还是MySql Server?

尝试:

GlobalConfiguration.Configuration.UseSqlServerStorage(ConfigurationManager.ConnectionStrings["MySqlConnection"].ConnectionString);

答案 1 :(得分:0)

我一直难以在网上找到合适的解决方案,然后问了一下,我意识到我应该自己在服务器上创建数据表,然后在应用程序启动后由hangfire创建表。

基本上,我在应用程序中添加了三行代码来使Hangfire工作正常运行:(。netcore Web应用程序)(在startup.cs中)

public void ConfigureServices(IServiceCollection services)
{
    ......
    string connectionString = "getYourConnectionStringHere";
    services.AddHangfire(x => x.UseSqlServerStorage(connectionString));
    services.AddHangfireServer();
    ...... 
}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ....
    app.UseHangfireDashboard("/hangfire"); //this is to set the url for hangfire, e.g. localhost://xxxx/hangfire
    ...
}