如何使用Mysql存储配置Hangfire

如何使用Mysql存储配置Hangfire,mysql,asp.net-core,hangfire,hangfire-sql,Mysql,Asp.net Core,Hangfire,Hangfire Sql,我有一个使用ASP.NET CORE开发的web应用程序,我想每周向客户发送时事通讯。环顾四周后,我认为Hangfire将是我使用的一个好工具。 不幸的是,我似乎无法使用Mysql数据库使其工作。它与InMemboryStorage完美配合 下面是我的设置 ASP.NET核心2.2 Hangfire 1.7.8 Hangfire.Mysql.Core 2.2.5 Hangfire在数据库中创建了大约9个表,但当我尝试创建后台作业时,。我得到一个错误,上面写着“hangfire_状态表不存在” 下

我有一个使用ASP.NET CORE开发的web应用程序,我想每周向客户发送时事通讯。环顾四周后,我认为Hangfire将是我使用的一个好工具。 不幸的是,我似乎无法使用Mysql数据库使其工作。它与InMemboryStorage完美配合

下面是我的设置

ASP.NET核心2.2

Hangfire 1.7.8

Hangfire.Mysql.Core 2.2.5

Hangfire在数据库中创建了大约9个表,但当我尝试创建后台作业时,。我得到一个错误,上面写着“hangfire_状态表不存在”

下面是我在启动时的配置,以及错误的堆栈跟踪。 如果你知道为什么会这样,请帮忙

谢谢

services.AddHangfire(cfg =>
            {
//                cfg.UseMemoryStorage();
                cfg.UseStorage(new MySqlStorage(
                    "server=localhost; database=hangfire; password=''; uid=root; port=3306; Allow User Variables=True"));
            });
为测试创建后台作业的控制器操作

[HttpGet("/hagnfire-test")]
        public IActionResult TestHangfire()
        {
            BackgroundJob.Enqueue(() => Console.WriteLine("Hangfire works!"));

            return Ok("Hangfire test");
        }
下面是错误

MySqlException: Table 'hangfire.hangfire_state' doesn't exist
MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in ResultSet.cs, line 49

MySqlException: Table 'hangfire.hangfire_state' doesn't exist
MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet() in MySqlDataReader.cs, line 116

BackgroundJobClientException: Background job creation failed. See inner exception for details.
Hangfire.BackgroundJobClient.Create(Job job, IState state)
堆栈跟踪

MySqlException: Table 'hangfire.hangfire_state' doesn't exist
MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet() in MySqlDataReader.cs
MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary<string, CachedProcedure> cachedProcedures, IMySqlCommand command, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in MySqlDataReader.cs
MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(IReadOnlyList<IMySqlCommand> commands, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in CommandExecutor.cs
MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in MySqlCommand.cs
MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() in MySqlCommand.cs
Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, ref CommandDefinition command, Action<IDbCommand, object> paramReader) in SqlMapper.cs
Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, ref CommandDefinition command) in SqlMapper.cs
Dapper.SqlMapper.Execute(IDbConnection cnn, string sql, object param, IDbTransaction transaction, Nullable<int> commandTimeout, Nullable<CommandType> commandType) in SqlMapper.cs
Hangfire.MySql.Core.MySqlWriteOnlyTransaction+<>c__DisplayClass7_0.<SetJobState>b__0(MySqlConnection x)
Hangfire.MySql.Core.MySqlWriteOnlyTransaction.<Commit>b__30_0(MySqlConnection connection)
Hangfire.MySql.Core.MySqlStorage+<>c__DisplayClass19_0.<UseTransaction>b__0(MySqlConnection connection)
Hangfire.MySql.Core.MySqlStorage+<>c__DisplayClass20_0<T>.<UseTransaction>b__0(MySqlConnection connection)
Hangfire.MySql.Core.MySqlStorage.UseConnection<T>(Func<MySqlConnection, T> func)
Hangfire.MySql.Core.MySqlStorage.UseTransaction<T>(Func<MySqlConnection, T> func, Nullable<IsolationLevel> isolationLevel)
Hangfire.MySql.Core.MySqlStorage.UseTransaction(Action<MySqlConnection> action)
Hangfire.Client.CoreBackgroundJobFactory+<>c__DisplayClass14_0.<Create>b__3(int attempt)
Hangfire.Client.CoreBackgroundJobFactory+<>c__DisplayClass15_0.<RetryOnException>b__0(int attempt)
Hangfire.Client.CoreBackgroundJobFactory.RetryOnException<T>(ref int attemptsLeft, Func<int, T> action)
Hangfire.Client.CoreBackgroundJobFactory.RetryOnException<T>(ref int attemptsLeft, Func<int, T> action)
Hangfire.Client.CoreBackgroundJobFactory.RetryOnException(ref int attemptsLeft, Action<int> action)
Hangfire.Client.CoreBackgroundJobFactory.Create(CreateContext context)
Hangfire.Client.BackgroundJobFactory+<>c__DisplayClass12_0.<CreateWithFilters>b__0()
Hangfire.Client.BackgroundJobFactory.InvokeClientFilter(IClientFilter filter, CreatingContext preContext, Func<CreatedContext> continuation)
Hangfire.Client.BackgroundJobFactory.Create(CreateContext context)
Hangfire.BackgroundJobClient.Create(Job job, IState state,
MySqlException:表'hangfire.hangfire_state'不存在
MySqlDataReader.cs中的MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet()
MySqlDataReader.cs中的MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(CommandListPosition CommandListPosition、ICommandPayloadCreator payloadCreator、IDictionary CachedProcess、IMySqlCommand命令、CommandBehavior行为、IOBehavior、CancellationToken CancellationToken)
CommandExecutor.cs中的MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(IReadOnlyList命令、ICommandPayloadCreator payloadCreator、CommandBehavior行为、IOBehavior IOBehavior、CancellationToken CancellationToken CancellationToken)
MySqlCommand.cs中的MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQueryAsync(IOBehavior IOBehavior,CancellationToken CancellationToken)
MySqlCommand.cs中的MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
SqlMapper.cs中的Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn,ref CommandDefinition命令,Action paramReader)
SqlMapper.cs中的Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn,ref CommandDefinition命令)
SqlMapper.cs中的Dapper.SqlMapper.Execute(IDbConnection cnn、字符串sql、对象参数、IDbTransaction transaction、Nullable commandTimeout、Nullable commandType)
Hangfire.MySql.Core.MySqlWriteOnlyTransaction+c__显示类7_0.b__0(MySqlConnection x)
Hangfire.MySql.Core.MySqlWriteOnlyTransaction.b_uu30_0(MySqlConnection)
Hangfire.MySql.Core.MySqlStorage+c__显示类19_0.b__0(MySqlConnection)
Hangfire.MySql.Core.MySqlStorage+c__显示类20_0.b__0(MySqlConnection)
Hangfire.MySql.Core.MySqlStorage.UseConnection(Func-Func)
Hangfire.MySql.Core.MySqlStorage.usetransation(Func-Func,可空的isolationLevel)
Hangfire.MySql.Core.MySqlStorage.UseTransaction(操作)
Hangfire.Client.CoreBackgroundJobFactory+c_uuuuDisplayClass14_0.b_uuu3(int尝试)
Hangfire.Client.CoreBackgroundJobFactory+c_uuuuDisplayClass15_0.b_uu0(int尝试)
Hangfire.Client.CoreBackgroundJobFactory.RetryOneException(ref int attemptsLeft,Func action)
Hangfire.Client.CoreBackgroundJobFactory.RetryOneException(ref int attemptsLeft,Func action)
Hangfire.Client.CoreBackgroundJobFactory.RetryOneException(ref int attempsleft,Action-Action)
Hangfire.Client.CoreBackgroundJobFactory.Create(CreateContext上下文)
Hangfire.Client.BackgroundJobFactory+c_uuuuDisplayClass12_0.b_uuu0()
Hangfire.Client.BackgroundJobFactory.InvokeClientFilter(IClientFilter筛选器、CreatingContext预文本、Func continuation)
Hangfire.Client.BackgroundJobFactory.Create(CreateContext上下文)
Hangfire.BackgroundJobClient.Create(作业、IState状态、,

您需要在
启动
配置中初始化属性
prepareschemaifrequired=true

通过以下选项:

var options =
new MySqlStorageOptions {
    TransactionIsolationLevel = IsolationLevel.ReadCommitted,
    QueuePollInterval = TimeSpan.FromSeconds(15),
    JobExpirationCheckInterval = TimeSpan.FromHours(1),
    CountersAggregateInterval = TimeSpan.FromMinutes(5),
    PrepareSchemaIfNecessary = true,
    DashboardJobListLimit = 50000
};
var storage = new MySqlStorage(connectionString, options);