hangfire支持mysql,如何为Hangfire的mySQL数据库设置'connectionString'?

I'm trying to integrate hangfire into my .NET core web app. I have followed the instructions on the Hangfire quickstart by installing all necessary packages. I also installed an extension called Hangfire MySql and installed the necessary packages for it.

Step 1 says to 'Create new instance of MySqlStorage with connection string constructor parameter and pass it to Configuration with UseStorage method:'

GlobalConfiguration.Configuration.UseStorage(

new MySqlStorage(connectionString));

Also it is noted that 'There must be Allow User Variables set to true in the connection string. For example: server=127.0.0.1;uid=root;pwd=root;database={0};Allow User Variables=True'

so my current code for Hangfire inside the 'Configure' service within my Startup.CS file is this:

Hangfire.GlobalConfiguration.Configuration.UseStorage(

new MySqlStorage(connectionString));

app.UseHangfireDashboard();

app.UseHangfireServer();

however MySqlStorage returns the error ''MySqlStorage' does not contain a constructor that takes 1 arguments'

Looking at the readMe for Hangfire mySQL if I use and define my connectionString to

e.g.

connectionString = "server=127.0.0.1;uid=root;pwd=root;database={0};Allow User Variables=True"

GlobalConfiguration.Configuration.UseStorage(

new MySqlStorage(

connectionString,

new MySqlStorageOptions

{

TablesPrefix = "Hangfire"

}));

the application will say there are no errors but I still get an error on startup.

I've tried entering a connection string but nothing that I enter seems to work. Every time I launch the application i get the error:

"crit: Microsoft.AspNetCore.Hosting.Internal.WebHost[6]

Application startup exception

System.InvalidOperationException: Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddHangfire' inside the call to 'ConfigureServices(...)' in the application startup code.

at Hangfire.HangfireApplicationBuilderExtensions.ThrowIfNotConfigured(IApplicationBuilder app)

at Hangfire.HangfireApplicationBuilderExtensions.UseHangfireDashboard(IApplicationBuilder app, String pathMatch, DashboardOptions options, JobStorage storage)

at Alerts.API.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) in /Users/Admin/Desktop/Code Projects/Alerts/Alerts.API/Startup.cs:line 178"

Wondering if someone could give me an example of how to set up Hangfire with a mySqlStorage connection that launches and let's me view the Hangfire dashboard.

解决方案

Based on the exception details, it seems that first you need to configure the Hangfire service before be able to call app.UseHangfireDashboard().

In your Startup.cs file you should have a ConfigureServices(IServiceCollection services) method, it seems that you must do the setup here instead of using the GlobalConfiguration class, so you can try this:

public void ConfigureServices(IServiceCollection services)

{

services.AddHangfire(configuration => {

configuration.UseStorage(

new MySqlStorage(

"server=127.0.0.1;uid=root;pwd=root;database={0};Allow User Variables=True",

new MySqlStorageOptions

{

TablesPrefix = "Hangfire"

}

)

);

};

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值