HangfireIO/Hangfire · error · ArgumentNullException
context
Error message
context
What it means
Thrown by BackgroundJobFactory.Create when the CreateContext argument is null. Create is the entry point of the client-side job-creation pipeline: it resolves IClientFilter instances, sets context.Factory = this, and dispatches OnCreating/OnCreated around the inner factory. The null-guard prevents NREs deep in the filter-dispatch and serialization logic. Hangfire's own IBackgroundJobClient.Create path always builds a CreateContext, so a null context implies a custom client or test.
Source
Thrown at src/Hangfire.Core/Client/BackgroundJobFactory.cs:76
}
}
internal BackgroundJobFactory(
[NotNull] IJobFilterProvider filterProvider,
[NotNull] IBackgroundJobFactory innerFactory)
{
if (filterProvider == null) throw new ArgumentNullException(nameof(filterProvider));
if (innerFactory == null) throw new ArgumentNullException(nameof(innerFactory));
_filterProvider = filterProvider;
_innerFactory = innerFactory;
}
public IStateMachine StateMachine => _innerFactory.StateMachine;
public BackgroundJob Create(CreateContext context)
{
if (context == null) throw new ArgumentNullException(nameof(context));
var filterInfo = GetFilters(context.Job);
try
{
context.Factory = this;
var createdContext = CreateWithFilters(context, filterInfo.ClientFilters);
return createdContext.BackgroundJob;
}
catch (Exception ex) when (ex.IsCatchableExceptionType())
{
var exceptionContext = new ClientExceptionContext(context, ex);
InvokeExceptionFilters(exceptionContext, filterInfo.ClientExceptionFiltersReversed);
if (!exceptionContext.ExceptionHandled)
{
throw;View on GitHub (pinned to c236dd0f93)
Solutions
- Construct a CreateContext with the required storage, connection, job, and initial state before calling Create.
- Use the standard IBackgroundJobClient.Create path (e.g., BackgroundJob.Enqueue/ContinueWith/AddOrUpdate) which builds the context for you.
- If you own a custom client, guard the context at your boundary and throw a descriptive exception.
Example fix
// before var job = factory.Create(null); // after var createContext = new CreateContext(storage, connection, jobObj, initialState); var job = factory.Create(createContext);
Defensive patterns
Strategy: validation
Validate before calling
if (createContext == null) throw new ArgumentException("CreateContext is required", nameof(createContext));
var job = factory.Create(createContext); Type guard
public static bool IsValidCreateContext(CreateContext ctx)
=> ctx != null && ctx.Job != null && ctx.Storage != null; Prevention
- Use IBackgroundJobClient.Enqueue/AddOrUpdate/ContinueWith rather than calling factory.Create directly.
- In custom clients, build a CreateContext with storage, connection, job, and initial state before invoking the factory.
- Guard custom dispatch paths with a null check on the context.
When it happens
Trigger: Invoking factory.Create(null) directly; a custom IBackgroundJobClient implementation that calls the factory without building a CreateContext; unit tests stubbing the factory without a context.
Common situations: Replacing BackgroundJobClient with a custom wrapper; integration tests that short-circuit the client; DI misconfiguration returning a null context from a factory delegate.
Related errors
- filterProvider
- stateMachine
- context
- exception
- Could not get a value of the job parameter `{name}`. See inn
AI-assisted analysis of HangfireIO/Hangfire@c236dd0f93 (2026-08-13).
Data as JSON: /api/errors/f653b1a6455a1213.
Report an issue: GitHub.