安装后未创建 hangfire 数据库

hangfire database not created after install

我用 Nuget 安装了 hangfire。

PM> Install-Package Hangfire

然后使用以下行更新 OWIN 启动 class:

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

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

我将 web.config 上的连接字符串名称命名为 UseSqlServerStorage 中的 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. 

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

的值

您使用的是 MsSql Server 还是 MySql 服务器?

尝试:

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

一直在网上苦苦寻找解决方案,后来问了一圈才知道应该自己在服务器上创建数据表,应用启动后通过hangfire创建表。

基本上,我在我的应用程序中放入了 3 行主要代码来使 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
    ...
}