HangfireIO/Hangfire · error · ArgumentNullException

connection

Error message

connection

What it means

Thrown by the JobActivatorContext constructor when the connection parameter is null. JobActivatorContext wraps the storage connection for use during job performance (setting/getting parameters); a null connection makes all storage operations impossible.

Source

Thrown at src/Hangfire.Core/JobActivatorContext.cs:30

// 
// 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

  1. Obtain the connection via storage.GetConnection() before constructing the context
  2. In tests, pass a mocked IStorageConnection instance
  3. If using PerformingContext, use context.Connection which is guaranteed non-null

Example fix

// before
var ctx = new JobActivatorContext(null, job, token);

// after
var connection = storage.GetConnection();
var ctx = new JobActivatorContext(connection, job, token);
Defensive patterns

Strategy: validation

Validate before calling

var connection = storage.GetConnection();
if (connection == null) throw new InvalidOperationException("Storage returned a null connection.");
var ctx = new JobActivatorContext(connection, backgroundJob, cancellationToken);

Type guard

connection != null && connection is IStorageConnection

Prevention

When it happens

Trigger: Constructing new JobActivatorContext(null, backgroundJob, cancellationToken) directly, or a code path where context.Connection is passed but the underlying IStorageConnection was not obtained.

Common situations: Custom filter or activator code that manually constructs a JobActivatorContext without calling storage.GetConnection(), or a test that forgets to mock the connection.

Related errors


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