HangfireIO/Hangfire · error · ArgumentNullException

existingConnection

Error message

existingConnection

What it means

The SqlServerStorage constructor received a null existingConnection argument. This is a standard ArgumentNullException guard at the top of the (DbConnection, SqlServerStorageOptions) constructor, thrown before the connection is stored.

Source

Thrown at src/Hangfire.SqlServer/SqlServerStorage.cs:117

        /// <summary>
        /// Initializes a new instance of the <see cref="SqlServerStorage"/> class with
        /// explicit instance of the <see cref="DbConnection"/> class that will be used
        /// to query the data.
        /// </summary>
        public SqlServerStorage([NotNull] DbConnection existingConnection)
            : this(existingConnection, new SqlServerStorageOptions())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SqlServerStorage"/> class with
        /// explicit instance of the <see cref="DbConnection"/> class that will be used
        /// to query the data, with the given options.
        /// </summary>
        public SqlServerStorage([NotNull] DbConnection existingConnection, [NotNull] SqlServerStorageOptions options)
        {
            if (existingConnection == null) throw new ArgumentNullException(nameof(existingConnection));
            if (options == null) throw new ArgumentNullException(nameof(options));

            _existingConnection = existingConnection;
            _options = options;

            Initialize();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SqlServerStorage"/> class with
        /// a connection factory <see cref="Func{DbConnection}"/> class that will be invoked
        /// to create new database connections for querying the data.
        /// </summary>
        public SqlServerStorage([NotNull] Func<DbConnection> connectionFactory)
            : this(connectionFactory, new SqlServerStorageOptions())
        {
        }

View on GitHub (pinned to c236dd0f93)

Solutions

  1. Ensure the DbConnection instance is created and non-null before passing it to the constructor.
  2. If using DI, register DbConnection with the correct lifetime (Singleton or Scoped) and connection string.
  3. Validate with a null check before construction if the source may be null.

Example fix

// before
DbConnection conn = _serviceProvider.GetService<DbConnection>(); // null if not registered
var storage = new SqlServerStorage(conn, options);
// after
services.AddSingleton<DbConnection>(sp =>
    new SqlConnection(Configuration.GetConnectionString("Hangfire")));
// ...
var storage = new SqlServerStorage(conn, options);
Defensive patterns

Strategy: validation

Validate before calling

DbConnection existingConnection = /* from DI or creation */;
if (existingConnection == null)
    throw new InvalidOperationException("existingConnection must be a non-null, open DbConnection.");

var storage = new SqlServerStorage(existingConnection, options);

Type guard

static bool IsUsableConnection(DbConnection conn) => conn != null && conn.State == ConnectionState.Open;

Prevention

When it happens

Trigger: Passing null to new SqlServerStorage(existingConnection, options); a DI-injected DbConnection that resolved to null.

Common situations: Dependency injection misconfiguration where the DbConnection service is not registered; conditional initialization that leaves the connection null; passing an already-disposed or never-created connection.

Related errors


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