{"record":{"id":"1105705067648f47","repo":"HangfireIO/Hangfire","slug":"unable-to-find-the-required-services-please-add-a","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/Hangfire.NetCore/HangfireServiceCollectionExtensions.cs","lineNumber":310,"sourceCode":"            where T : class\n        {\n            serviceCollection.TryAddSingleton<T>(serviceProvider =>\n            {\n                if (serviceProvider == null) throw new ArgumentNullException(nameof(serviceProvider));\n\n                // ensure the configuration was performed\n                serviceProvider.GetRequiredService<IGlobalConfiguration>();\n\n                return implementationFactory(serviceProvider);\n            });\n        }\n\n        public static void ThrowIfNotConfigured(IServiceProvider serviceProvider)\n        {\n            var configuration = serviceProvider.GetService<IGlobalConfiguration>();\n            if (configuration == null)\n            {\n                throw new InvalidOperationException(\n                    \"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.\");\n            }\n        }\n    }\n}\n","sourceCodeStart":292,"sourceCodeEnd":316,"githubUrl":"https://github.com/HangfireIO/Hangfire/blob/c236dd0f930f831ec151e436e138ddc429a02a72/src/Hangfire.NetCore/HangfireServiceCollectionExtensions.cs#L292-L316","documentation":"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.","triggerScenarios":"Calling ThrowIfNotConfigured(serviceProvider) — invoked by Hangfire's hosted service / middleware at startup — before AddHangfire was registered on that service collection.","commonSituations":"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.","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)."],"exampleFix":"// before\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddControllers();\n    // Hangfire not registered\n}\n\n// after\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddControllers();\n    services.AddHangfire(cfg => cfg.UseSqlServerStorage(Configuration.GetConnectionString(\"Hangfire\")));\n    services.AddHangfireServer();\n}","handlingStrategy":"validation","validationCode":"// in a startup test:\nvar sp = services.BuildServiceProvider();\nif (sp.GetService<IGlobalConfiguration>() == null)\n    throw new InvalidOperationException(\"AddHangfire was not called in ConfigureServices.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["di","startup","configuration","netcore"],"backgroundTag":null,"analyzedSha":"c236dd0f930f831ec151e436e138ddc429a02a72","analyzedAt":"2026-08-13T20:27:11.027Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}