{"record":{"id":"9abbc51289d26f42","repo":"HangfireIO/Hangfire","slug":"compatibilitylevel","errorCode":null,"errorMessage":"compatibilityLevel","messagePattern":"compatibilityLevel","errorType":"validation","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Hangfire.Core/GlobalConfiguration.cs","lineNumber":65,"sourceCode":"            return CompatibilityLevel >= level;\n        }\n\n        internal GlobalConfiguration()\n        {\n        }\n    }\n\n    public static class CompatibilityLevelExtensions\n    {\n        public static IGlobalConfiguration SetDataCompatibilityLevel(\n            [NotNull] this IGlobalConfiguration configuration,\n            CompatibilityLevel compatibilityLevel)\n        {\n            if (configuration == null) throw new ArgumentNullException(nameof(configuration));\n\n#if !NETSTANDARD1_3\n            if (!Enum.IsDefined(typeof(CompatibilityLevel), compatibilityLevel))\n                throw new InvalidEnumArgumentException(nameof(compatibilityLevel), (int) compatibilityLevel,\n                    typeof(CompatibilityLevel));\n#endif\n\n            GlobalConfiguration.CompatibilityLevel = compatibilityLevel;\n\n            return configuration;\n        }\n    }\n}\n","sourceCodeStart":47,"sourceCodeEnd":75,"githubUrl":"https://github.com/HangfireIO/Hangfire/blob/c236dd0f930f831ec151e436e138ddc429a02a72/src/Hangfire.Core/GlobalConfiguration.cs#L47-L75","documentation":"SetDataCompatibilityLevel validates the compatibilityLevel argument with Enum.IsDefined and throws InvalidEnumArgumentException(nameof(compatibilityLevel), (int)compatibilityLevel, typeof(CompatibilityLevel)) at GlobalConfiguration.cs:65 (on non-NETSTANDARD1_3 targets). The CompatibilityLevel enum only defines Version_110, Version_170, Version_180, so any other integer value is rejected.","triggerScenarios":"Passing an undefined enum value, e.g. ((CompatibilityLevel)999), or casting an arbitrary int loaded from config straight to CompatibilityLevel and forwarding it. Also when a future/removed enum member from another Hangfire version is round-tripped.","commonSituations":"Reading the level from appsettings.json as an int and casting without validation; typos like Version_150; mixing Hangfire versions where the enum members differ; default(int) (0) passed because the value was never set.","solutions":["Validate with Enum.IsDefined(typeof(CompatibilityLevel), value) before calling, and fall back to a known level.","Use the named enum member (CompatibilityLevel.Version_170) instead of integer casts.","Bind configuration to the enum type directly so invalid strings fail at config-bind time rather than at runtime.","Whitelist the allowed values and reject/log anything else during startup."],"exampleFix":"// before\nvar lvl = (CompatibilityLevel)configSection.Value; // 0 or 999\ncfg.SetDataCompatibilityLevel(lvl);\n// after\nvar raw = configSection.Value;\nvar lvl = Enum.IsDefined(typeof(CompatibilityLevel), raw)\n    ? (CompatibilityLevel)raw\n    : CompatibilityLevel.Version_170;\ncfg.SetDataCompatibilityLevel(lvl);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(CompatibilityLevel), value))\n{\n    throw new ArgumentOutOfRangeException(\n        nameof(value), value, \"Unsupported CompatibilityLevel.\");\n}\nconfiguration.SetDataCompatibilityLevel((CompatibilityLevel)value);","typeGuard":"static bool IsValidLevel(int v) =>\n    Enum.IsDefined(typeof(CompatibilityLevel), v);","tryCatchPattern":null,"preventionTips":["Bind config values to the CompatibilityLevel enum, not to int, so invalid text fails at bind time.","Whitelist Version_110/170/180 and reject the rest at startup.","Never cast arbitrary ints to the enum without Enum.IsDefined.","Pin a single compatibility level across all nodes."],"tags":["csharp","hangfire","invalid-enum","configuration","compatibility"],"backgroundTag":null,"analyzedSha":"c236dd0f930f831ec151e436e138ddc429a02a72","analyzedAt":"2026-08-13T20:27:11.027Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}