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
- Add services.AddHangfire(cfg => cfg.UseXxxStorage(...)) inside ConfigureServices before any Hangfire middleware/hosted service.
- Ensure AddHangfire and AddHangfireServer are called on the same IServiceCollection that builds the app's IServiceProvider.
- 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
- Register AddHangfire and AddHangfireServer in the same ConfigureServices that builds the app container.
- Avoid environment branches that silently skip Hangfire registration.
- Add a smoke test that resolves IGlobalConfiguration after ConfigureServices.
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
- Current JobStorage instance has not been initialized yet. Yo
- client
- storage
- Attempts value must be equal or greater than zero.
- DelaysInSeconds value must be an array of non-negative numbe
AI-assisted analysis of HangfireIO/Hangfire@c236dd0f93 (2026-08-13).
Data as JSON: /api/errors/1105705067648f47.
Report an issue: GitHub.