HangfireIO/Hangfire · error · ArgumentNullException
backgroundJob
Error message
backgroundJob
What it means
Thrown by the JobActivatorContext constructor when the backgroundJob parameter is null. The context exposes BackgroundJob.Id for parameter operations and BackgroundJob.ParametersSnapshot for stale reads; a null value would cause downstream NullReferenceException.
Source
Thrown at src/Hangfire.Core/JobActivatorContext.cs:31
// You should have received a copy of the GNU Lesser General Public
// License along with Hangfire. If not, see <http://www.gnu.org/licenses/>.
using System;
using Hangfire.Annotations;
using Hangfire.Common;
using Hangfire.Storage;
namespace Hangfire
{
public class JobActivatorContext
{
public JobActivatorContext(
[NotNull] IStorageConnection connection,
[NotNull] BackgroundJob backgroundJob,
[NotNull] IJobCancellationToken cancellationToken)
{
if (connection == null) throw new ArgumentNullException(nameof(connection));
if (backgroundJob == null) throw new ArgumentNullException(nameof(backgroundJob));
if (cancellationToken == null) throw new ArgumentNullException(nameof(cancellationToken));
Connection = connection;
BackgroundJob = backgroundJob;
CancellationToken = cancellationToken;
}
[NotNull]
public BackgroundJob BackgroundJob { get; }
[NotNull]
public IJobCancellationToken CancellationToken { get; }
[NotNull]
public IStorageConnection Connection { get; }
public void SetJobParameter([NotNull] string name, object value)
{View on GitHub (pinned to c236dd0f93)
Solutions
- Always pass a valid BackgroundJob loaded from storage or constructed with a valid Id
- In tests, create a BackgroundJob with the required fields populated
- Use PerformingContext.BackgroundJob rather than manually constructing the context
Example fix
// before var ctx = new JobActivatorContext(conn, null, token); // after var job = conn.GetBackgroundJob(jobId); var ctx = new JobActivatorContext(conn, job, token);
Defensive patterns
Strategy: validation
Validate before calling
var job = connection.GetBackgroundJob(jobId);
if (job == null) throw new InvalidOperationException($"BackgroundJob {jobId} not found in storage.");
var ctx = new JobActivatorContext(connection, job, cancellationToken); Type guard
backgroundJob != null && backgroundJob is BackgroundJob
Prevention
- Load BackgroundJob from storage before constructing JobActivatorContext
- In tests, create a BackgroundJob with all required fields populated
- Prefer using PerformingContext.BackgroundJob rather than manual construction
When it happens
Trigger: Constructing new JobActivatorContext(connection, null, cancellationToken) directly, or passing a BackgroundJob that was not properly loaded from storage.
Common situations: Custom server filter or test code that constructs a JobActivatorContext manually without fetching the BackgroundJob from the connection.
Related errors
AI-assisted analysis of HangfireIO/Hangfire@c236dd0f93 (2026-08-13).
Data as JSON: /api/errors/457feccc04abc9e7.
Report an issue: GitHub.