HangfireIO/Hangfire · error · ArgumentNullException
configuration
Error message
configuration
What it means
SetDataCompatibilityLevel is a fluent extension on IGlobalConfiguration that stores the data-serialization compatibility level. It throws ArgumentNullException(nameof(configuration)) at GlobalConfiguration.cs:61 when the receiver is null, because it must return that same configuration to keep the fluent chain alive.
Source
Thrown at src/Hangfire.Core/GlobalConfiguration.cs:61
}
internal static bool HasCompatibilityLevel(CompatibilityLevel level)
{
return CompatibilityLevel >= level;
}
internal GlobalConfiguration()
{
}
}
public static class CompatibilityLevelExtensions
{
public static IGlobalConfiguration SetDataCompatibilityLevel(
[NotNull] this IGlobalConfiguration configuration,
CompatibilityLevel compatibilityLevel)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
#if !NETSTANDARD1_3
if (!Enum.IsDefined(typeof(CompatibilityLevel), compatibilityLevel))
throw new InvalidEnumArgumentException(nameof(compatibilityLevel), (int) compatibilityLevel,
typeof(CompatibilityLevel));
#endif
GlobalConfiguration.CompatibilityLevel = compatibilityLevel;
return configuration;
}
}
}
View on GitHub (pinned to c236dd0f93)
Solutions
- Start the fluent chain from GlobalConfiguration.Configuration, which is a never-null singleton (GlobalConfiguration.cs:37).
- If you hold the config in a field/parameter, null-check it before chaining.
- When wiring through DI, ensure the factory always returns a non-null IGlobalConfiguration.
- Build the whole configuration chain in one statement so no intermediate variable can be null.
Example fix
// before
IGlobalConfiguration cfg = null;
cfg.SetDataCompatibilityLevel(CompatibilityLevel.Version_170);
// after
GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170); Defensive patterns
Strategy: validation
Validate before calling
// always start from the never-null singleton
GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170);
// or, with a captured variable:
if (configuration == null)
throw new ArgumentNullException(nameof(configuration));
configuration.SetDataCompatibilityLevel(CompatibilityLevel.Version_170); Type guard
static bool IsConfigured(IGlobalConfiguration? c) => c != null;
Prevention
- Boot all Hangfire configuration from GlobalConfiguration.Configuration.
- Never accept an IGlobalConfiguration from DI that can be null.
- Set the compatibility level exactly once at startup.
- Enable nullable reference types.
When it happens
Trigger: Invoking ((IGlobalConfiguration)null).SetDataCompatibilityLevel(CompatibilityLevel.Version_170), or storing the config in a field that is later null and calling .SetDataCompatibilityLevel on it.
Common situations: Not bootstrapping configuration from the singleton GlobalConfiguration.Configuration; assigning the IGlobalConfiguration from a DI factory that can return null; calling UseXxx before the config object is created.
Related errors
AI-assisted analysis of HangfireIO/Hangfire@c236dd0f93 (2026-08-13).
Data as JSON: /api/errors/9780ffd08d95f345.
Report an issue: GitHub.