问题描述
我正在尝试将 hangfire 集成到我的 .NET 核心 Web 应用程序中.我按照 Hangfire 快速入门的说明安装了所有必要的软件包.我还安装了一个名为 Hangfire MySql 的扩展并为其安装了必要的包.
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.
第 1 步说使用连接字符串构造函数参数创建 MySqlStorage 的新实例并将其传递给使用 UseStorage 方法的配置:"
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));
还要注意的是,'必须在连接字符串中将允许用户变量设置为 true.例如:server=127.0.0.1;uid=root;pwd=root;database={0};Allow User Variables=True'
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'
所以我当前在 Startup.CS 文件中的配置"服务中的 Hangfire 代码是这样的:
so my current code for Hangfire inside the 'Configure' service within my Startup.CS file is this:
Hangfire.GlobalConfiguration.Configuration.UseStorage(new MySqlStorage(connectionString));
Hangfire.GlobalConfiguration.Configuration.UseStorage( new MySqlStorage(connectionString));
app.UseHangfireDashboard();
app.UseHangfireServer();
然而,MySqlStorage 返回错误''MySqlStorage' 不包含采用 1 个参数的构造函数'
however MySqlStorage returns the error ''MySqlStorage' does not contain a constructor that takes 1 arguments'
查看 Hangfire mySQL 的自述文件,如果我使用并将 connectionString 定义为
Looking at the readMe for Hangfire mySQL if I use and define my connectionString to
例如
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:
"暴击:Microsoft.AspNetCore.Hosting.Internal.WebHost[6]应用程序启动异常System.InvalidOperationException: 无法找到所需的服务.请通过在应用程序启动代码中对ConfigureServices(...)"的调用中调用IServiceCollection.AddHangfire"来添加所有必需的服务.在 Hangfire.HangfireApplicationBuilderExtensions.ThrowIfNotConfigured(IApplicationBuilder 应用程序)在 Hangfire.HangfireApplicationBuilderExtensions.UseHangfireDashboard(IApplicationBuilder app, String pathMatch, DashboardOptions options, JobStorage storage)在 Alerts.API.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 在/Users/Admin/Desktop/Code Projects/Alerts/Alerts.API/Startup.cs:line 178"
"crit: Microsoft.AspNetCore.Hosting.Internal.WebHost[6] Application startup exceptionSystem.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"
想知道是否有人可以给我一个示例,说明如何使用启动的 mySqlStorage 连接设置 Hangfire,让我查看 Hangfire 仪表板.
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.
参考:https://github.com/arnoldasgudas/Hangfire.MySqlStorageHangfire:http://docs.hangfire.io/en/latest/quick-开始.html
References: https://github.com/arnoldasgudas/Hangfire.MySqlStorageHangfire: http://docs.hangfire.io/en/latest/quick-start.html
推荐答案
根据异常详情,看来需要先配置Hangfire服务才能调用app.UseHangfireDashboard().
Based on the exception details, it seems that first you need to configure the Hangfire service before be able to call app.UseHangfireDashboard().
在您的 Startup.cs 文件中,您应该有一个 ConfigureServices(IServiceCollection services) 方法,看来您必须在此处进行设置而不是使用 GlobalConfiguration 类,因此您可以尝试:
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"
}
)
);
};
}
这篇关于如何为 Hangfire 的 mySQL 数据库设置“connectionString"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!