HangfireIO/Hangfire · error · ArgumentNullException

client

Error message

client

What it means

The Schedule<T> extension (with an explicit queue) is a null-check guard: it throws ArgumentNullException with paramName 'client' when the IBackgroundJobClient argument is null. The message field shows just 'client' because that is the parameter name. The method then delegates to client.Create with a ScheduledState. This is the standard extension-method null guard — extension methods run on a null 'this' and do not use instance dispatch.

Source

Thrown at src/Hangfire.Core/BackgroundJobClientExtensions.cs:485

        }

        /// <summary>
        /// Creates a new background job based on a specified lambda expression and schedules
        /// it to be enqueued to the specified queue at the specified moment.
        /// </summary>
        /// <typeparam name="T">Type whose method will be invoked during job processing.</typeparam>
        /// <param name="client">A job client instance.</param>
        /// <param name="queue">Default queue for the background job.</param>
        /// <param name="methodCall">Method call expression that will be marshalled to the Server.</param>
        /// <param name="enqueueAt">Moment at which the job will be enqueued.</param>
        /// <returns>Unique identifier of a created job.</returns>
        public static string Schedule<T>(
            [NotNull] this IBackgroundJobClient client,
            [NotNull] string queue,
            [NotNull, InstantHandle] Expression<Func<T, Task>> methodCall,
            DateTimeOffset enqueueAt)
        {
            if (client == null) throw new ArgumentNullException(nameof(client));
            return client.Create(queue, methodCall, new ScheduledState(enqueueAt.UtcDateTime));
        }

        /// <summary>
        /// Creates a new background job based on a specified lambda expression in a given state.
        /// </summary>
        /// <param name="client">A job client instance.</param>
        /// <param name="methodCall">Static method call expression that will be marshalled to the Server.</param>
        /// <param name="state">Initial state of a job.</param>
        /// <returns>Unique identifier of the created job.</returns>
        public static string Create(
            [NotNull] this IBackgroundJobClient client,
            [NotNull, InstantHandle] Expression<Action> methodCall,
            [NotNull] IState state)
        {
            if (client == null) throw new ArgumentNullException(nameof(client));

            return client.Create(Job.FromExpression(methodCall), state);

View on GitHub (pinned to c236dd0f93)

Solutions

  1. Ensure IBackgroundJobClient is registered — call GlobalConfiguration.Configuration.UseXXXStorage() before using the static BackgroundJob API, or inject IBackgroundJobClient via DI.
  2. Verify JobStorage.Current is non-null before using static helpers.
  3. If constructing manually, pass a valid JobStorage to new BackgroundJobClient(storage).
  4. Null-check the client before calling the extension if its source is uncertain.

Example fix

// before
IBackgroundJobClient client = null;
client.Schedule<MyService>("queue", x => x.DoAsync(), at); // throws

// after
var client = new BackgroundJobClient(JobStorage.Current);
client.Schedule<MyService>("queue", x => x.DoAsync(), at);
Defensive patterns

Strategy: validation

Validate before calling

if (client == null) throw new InvalidOperationException("IBackgroundJobClient not configured");
client.Schedule<T>(queue, expr, enqueueAt);

Type guard

static bool IsClientReady(IBackgroundJobClient client) => client != null && JobStorage.Current != null;

Prevention

When it happens

Trigger: Calling client.Schedule<T>(queue, expr, enqueueAt) where 'client' resolved to null — e.g., DI did not register IBackgroundJobClient, the static JobStorage.Current is unconfigured (so BackgroundJob.Client is null), or a field/property was never initialized.

Common situations: Forgot to call services.AddHangfire(...)/AddHangfireServer in ASP.NET Core DI; using new BackgroundJobClient() before GlobalConfiguration.UseXXXStorage; accessing the client in a context without DI scope; calling the static BackgroundJob.Schedule before JobStorage is configured.

Related errors


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