HangfireIO/Hangfire · critical · InvalidOperationException

Unable to find the required services. Please add all the req

Error message

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.

What it means

InvalidOperationException thrown by HangfireServiceCollectionExtensions.ThrowIfNotConfigured when the IServiceProvider cannot resolve IGlobalConfiguration. Hangfire's ASP.NET Core integration requires AddHangfire(...) to be called in ConfigureServices; if it was skipped (or called against a different IServiceCollection), the marker global configuration service is absent and the middleware/hosted services refuse to start.

Source

Thrown at src/Hangfire.NetCore/HangfireServiceCollectionExtensions.cs:310

            where T : class
        {
            serviceCollection.TryAddSingleton<T>(serviceProvider =>
            {
                if (serviceProvider == null) throw new ArgumentNullException(nameof(serviceProvider));

                // ensure the configuration was performed
                serviceProvider.GetRequiredService<IGlobalConfiguration>();

                return implementationFactory(serviceProvider);
            });
        }

        public static void ThrowIfNotConfigured(IServiceProvider serviceProvider)
        {
            var configuration = serviceProvider.GetService<IGlobalConfiguration>();
            if (configuration == null)
            {
                throw new 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.");
            }
        }
    }
}

View on GitHub (pinned to c236dd0f93)

Solutions

  1. Add services.AddHangfire(cfg => cfg.UseXxxStorage(...)) inside ConfigureServices before any Hangfire middleware/hosted service.
  2. Ensure AddHangfire and AddHangfireServer are called on the same IServiceCollection that builds the app's IServiceProvider.
  3. Verify the registration line actually executes for the current environment (not behind an env check that returned false).

Example fix

// before
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    // Hangfire not registered
}

// after
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddHangfire(cfg => cfg.UseSqlServerStorage(Configuration.GetConnectionString("Hangfire")));
    services.AddHangfireServer();
}
Defensive patterns

Strategy: validation

Validate before calling

// in a startup test:
var sp = services.BuildServiceProvider();
if (sp.GetService<IGlobalConfiguration>() == null)
    throw new InvalidOperationException("AddHangfire was not called in ConfigureServices.");

Prevention

When it happens

Trigger: Calling ThrowIfNotConfigured(serviceProvider) — invoked by Hangfire's hosted service / middleware at startup — before AddHangfire was registered on that service collection.

Common situations: Forgetting services.AddHangfire(...) in Startup.ConfigureServices; registering Hangfire on a different container or a child scope; conditionally adding Hangfire in an environment branch that was not taken; moving config into a module that wasn't referenced.

Related errors


AI-assisted analysis of HangfireIO/Hangfire@c236dd0f93 (2026-08-13). Data as JSON: /api/errors/1105705067648f47. Report an issue: GitHub.