Search code examples
c#asp.net.net.net-corehangfire

How do I set up 'connectionString' for a mySQL database for Hangfire?


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.

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));

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'

so my current code for Hangfire inside the 'Configure' service within my Startup.CS file is this:

Hangfire.GlobalConfiguration.Configuration.UseStorage( new MySqlStorage(connectionString));

    app.UseHangfireDashboard();
    app.UseHangfireServer();

however MySqlStorage returns the error ''MySqlStorage' does not contain a constructor that takes 1 arguments'

Looking at the readMe for Hangfire mySQL if I use and define my connectionString to

e.g.

 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:

"crit: Microsoft.AspNetCore.Hosting.Internal.WebHost[6] Application startup exception System.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"

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.

References: https://github.com/arnoldasgudas/Hangfire.MySqlStorage Hangfire: http://docs.hangfire.io/en/latest/quick-start.html


Solution

  • Based on the exception details, it seems that first you need to configure the Hangfire service before be able to call app.UseHangfireDashboard().

    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"
                    }
                )
            );
        };
    }