HangfireIO/Hangfire · error · InvalidOperationException

Could not set parameter for a created job.

Error message

Could not set parameter for a created job.

What it means

InvalidOperationException thrown unconditionally by CreatedContext.SetJobParameter, which is marked [Obsolete] and documented to only throw. After a job is created (OnCreated phase), parameters can no longer be set — the job is already persisted with its serialized parameters. This stub exists for backward API compatibility and will be removed in 2.0.0. Calling it is always an error; use CreatingContext.SetJobParameter during the OnCreating phase instead.

Source

Thrown at src/Hangfire.Core/Client/CreatedContext.cs:73

        /// <summary>
        /// Gets a value that indicates that this <see cref="CreatedContext"/>
        /// object was canceled.
        /// </summary>
        public bool Canceled { get; }

        /// <summary>
        /// Gets or sets a value that indicates that this <see cref="CreatedContext"/>
        /// object handles an exception occurred during the creation of the job.
        /// </summary>
        public bool ExceptionHandled { get; set; }

        [Obsolete("This method only throws InvalidOperationException, will be removed in 2.0.0.")]
        public void SetJobParameter([NotNull] string name, object value)
        {
            if (String.IsNullOrWhiteSpace(name)) throw new ArgumentNullException(nameof(name));

            throw new InvalidOperationException("Could not set parameter for a created job.");
        }
    }
}

View on GitHub (pinned to c236dd0f93)

Solutions

  1. Move SetJobParameter calls into the OnCreating phase, using CreatingContext.SetJobParameter.
  2. If the data must be attached after creation, store it in context.Items (in-process only) or write a separate job parameter via a storage transaction.
  3. Delete calls to CreatedContext.SetJobParameter entirely — it never succeeds.

Example fix

// before (OnCreated — always throws)
public void OnCreated(CreatedContext context) {
    context.SetJobParameter("Tag", "value");
}

// after (OnCreating — correct phase)
public void OnCreating(CreatingContext context) {
    context.SetJobParameter("Tag", "value");
}
Defensive patterns

Strategy: validation

Validate before calling

// Never call CreatedContext.SetJobParameter — it always throws.
// Move all SetJobParameter calls to OnCreating where CreatingContext.SetJobParameter works.

Try / catch

try { createdContext.SetJobParameter("k", v); }
catch (InvalidOperationException) { /* migrate to OnCreating */ }

Prevention

When it happens

Trigger: Calling createdContext.SetJobParameter("key", value) inside an IClientFilter.OnCreated handler or any code holding a CreatedContext. Also triggered by migrating old code that called SetJobParameter on the post-creation context.

Common situations: Legacy filters written against an older API that allowed parameter mutation after creation; copy-paste from OnCreating logic into OnCreated; filters that try to stamp metadata onto the job after it is already stored.

Related errors


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