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

1

5

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

Mysterious answered 27/1, 2019 at 3:4 Comment(0)
D
8

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"
                }
            )
        );
    };
}
Diversiform answered 27/1, 2019 at 3:31 Comment(3)
Thankyou!! This worked perfectly, appreciate the help. Is there any reason why the documentation does not specifically say to use "services.AddHangfire"? If it wasn't for you it would have taken me days to figure this out(new to c#) thanks again!Mysterious
I'm glad this was helpful. If you look closer to the exception details you provided, the library is telling you to do the service registration there using the AddHangfire extension method. It seems they made that change to the code base but the formal documentation haven't been updated. I just found this in the main Hangfire repo: github.com/HangfireIO/Hangfire/blob/master/content/readme.txtDiversiform
ok cool, thank you again! I guess next time I run into an error like this I'll have to dig a little deeper and check out the repo documentation.Mysterious

© 2022 - 2024 — McMap. All rights reserved.