首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Mysql存储配置Hangfire

如何使用Mysql存储配置Hangfire
EN

Stack Overflow用户
提问于 2020-01-10 16:09:48
回答 1查看 992关注 0票数 3

我有一个网络应用程序,我开发使用ASP.NET核心,我想发送每周的时事通讯给我的客户。环顾四周后,我认为Hangfire将是我使用的一个很好的工具。不幸的是,我似乎不能让它使用Mysql数据库工作。它与InMemboryStorage完美配合。

下面是我的设置。

ASP.NET核心2.2

Hangfire 1.7.8

Hangfire.Mysql.Core 2.2.5

Hangfire在数据库中创建了大约9个表,但是当我尝试创建一个后台作业时。我得到一个错误,说"hangfire_state表不存在“。

下面是我在启动时的配置,以及错误的堆栈跟踪。如果您知道为什么会发生这种情况,请提供帮助。

谢谢!

代码语言:javascript
复制
services.AddHangfire(cfg =>
            {
//                cfg.UseMemoryStorage();
                cfg.UseStorage(new MySqlStorage(
                    "server=localhost; database=hangfire; password=''; uid=root; port=3306; Allow User Variables=True"));
            });

创建用于测试的后台作业的控制器操作

代码语言:javascript
复制
[HttpGet("/hagnfire-test")]
        public IActionResult TestHangfire()
        {
            BackgroundJob.Enqueue(() => Console.WriteLine("Hangfire works!"));

            return Ok("Hangfire test");
        }

下面是错误;

代码语言:javascript
复制
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)

堆栈跟踪

代码语言:javascript
复制
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,
EN

回答 1

Stack Overflow用户

发布于 2021-04-22 21:58:52

您需要在StartUp配置中初始化属性PrepareSchemaIfNecessary = true

传递以下选项:

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59677653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档