HangfireIO/Hangfire · error · ArgumentNullException

context

Error message

context

What it means

Thrown by CaptureCultureAttribute.OnCreating when the CreatingContext argument is null. This is a standard null-guard (ArgumentNullException) protecting the IClientFilter.OnCreating pipeline that captures the current thread's CurrentCulture and CurrentUICulture into job parameters before enqueueing. Hangfire internally invokes OnCreating during job creation via its filter pipeline, so a null context indicates a programming error in a custom filter provider or test harness, not a runtime/infrastructure issue.

Source

Thrown at src/Hangfire.Core/CaptureCultureAttribute.cs:76

        [CanBeNull]
        public string DefaultUICultureName { get; }

        public bool CaptureDefault { get; }

#if !NETSTANDARD1_3
        /// <summary>
        /// Gets or sets whether to use the <see cref="GetCultureInfo"/> method when getting
        /// a culture by its name, or create a <see cref="CultureInfo"/> instance using its
        /// constructor instead. Cached method does not respect user-overridden values associated
        /// with the current culture specified on the OS level.
        /// </summary>
        public bool CachedCulture { get; set; }
#endif

        public void OnCreating(CreatingContext context)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));

            var currentCulture = CultureInfo.CurrentCulture;
            var currentUICulture = CultureInfo.CurrentUICulture;

            if (CaptureDefault == false && currentCulture.Name.Equals(DefaultCultureName, StringComparison.Ordinal))
            {
                // Don't set the 'CurrentCulture' job parameter when it's equal to the default one
            }
            else
            {
                context.SetJobParameter("CurrentCulture", currentCulture.Name);
            }

            if (CaptureDefault == false && currentUICulture.Name.Equals(DefaultUICultureName, StringComparison.Ordinal))
            {
                // Don't set the 'CurrentUICulture' job parameter when it's equal to the default one
            }
            else if (GlobalConfiguration.HasCompatibilityLevel(CompatibilityLevel.Version_180) &&

View on GitHub (pinned to c236dd0f93)

Solutions

  1. Ensure OnCreating is never called directly by application code — rely on Hangfire's filter pipeline (BackgroundJob.Create -> BackgroundJobFactory.Create -> filter dispatch) which always supplies a non-null context.
  2. If invoking the filter manually (e.g., in a test), construct a valid CreatingContext from a CreateContext with a real or mock IStorageConnection, Job, and initial state.
  3. Add a null check in your own caller before dispatching to OnCreating and fail with a descriptive error upstream.

Example fix

// before (broken test/manual call)
var attr = new CaptureCultureAttribute();
attr.OnCreating(null);

// after — construct a real context
var createContext = new CreateContext(storage, connection, job, initialState);
var creatingContext = new CreatingContext(createContext);
attr.OnCreating(creatingContext);
Defensive patterns

Strategy: validation

Validate before calling

if (context == null) throw new ArgumentException("CreatingContext is required before invoking CaptureCultureAttribute.OnCreating", nameof(context));
// or guard in your dispatch code
if (creatingContext == null) return;
attr.OnCreating(creatingContext);

Type guard

public static bool IsValidCreatingContext(CreatingContext ctx)
    => ctx != null && ctx.Job != null && ctx.Items != null;

Prevention

When it happens

Trigger: Calling new CaptureCultureAttribute().OnCreating(null) directly, or a custom IClientFilter provider that manually dispatches the attribute's OnCreating with a null context. Also reproducible in unit tests that instantiate the attribute and invoke the filter method without constructing a real CreatingContext.

Common situations: Writing unit tests for a custom job filter that wraps CaptureCultureAttribute; integrating a custom IBackgroundJobClient or IJobFilterProvider that constructs filter invocation pipelines manually; reflection-based invocation that passes null due to a missing context object.

Related errors


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