3

I have a web app that I developed using ASP.NET CORE and I want to send weekly newsletters to my customers. After looking around, I thought Hangfire will be a good tool for me to use. Unfortunately, I can't seem to get it to work using Mysql database. It works perfectly with the InMemboryStorage.

Below is my set up.

ASP.NET CORE 2.2

Hangfire 1.7.8

Hangfire.Mysql.Core 2.2.5

Hangfire creates about 9 tables in the database, but when I try creating a background job,. I get an error that says, "hangfire_state table doesn't exist".

Below is my configuration in the startup, and the error's stack trace. Please help out if you have any idea why this is happening.

Thanks!

services.AddHangfire(cfg =>
            {
//                cfg.UseMemoryStorage();
                cfg.UseStorage(new MySqlStorage(
                    "server=localhost; database=hangfire; password=''; uid=root; port=3306; Allow User Variables=True"));
            });

The controller action that creates a background job for testing

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

            return Ok("Hangfire test");
        }

Below is the error;

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)

The stack trace

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,

1 Answers1

1

You need to initialize the property PrepareSchemaIfNecessary = true in your StartUp configuration.

Pass the following options:

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);
ザヘド
  • 614
  • 1
  • 6
  • 17
  • Please note this MySQL driver does not support MySQL v8 (only 5.7 is supported). You will get blank dashboard pages if you try to use it with v8. – Scottish Smile Sep 01 '23 at 00:44