问题描述
我正在尝试将hangfire集成到我的.NET核心网络应用中。我已通过安装所有必需的软件包来遵循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的自述文件
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上的。 / Users / Admin / Desktop / Code Projects / Alerts / Alerts.API / Startup.cs:line 178中的ILoggerFactory loggerFactory)
"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.
参考文献:
Hangfire:
推荐答案
根据异常详细信息,似乎首先需要配置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'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!