HangfireIO/Hangfire · error · ArgumentNullException

connectionFactory

Error message

connectionFactory

What it means

The UseSqlServerStorage(this IGlobalConfiguration, Func<DbConnection>, SqlServerStorageOptions) extension method received a null connectionFactory argument. This is a standard ArgumentNullException guard for the factory delegate parameter.

Source

Thrown at src/Hangfire.SqlServer/SqlServerStorageExtensions.cs:70

        public static IGlobalConfiguration<SqlServerStorage> UseSqlServerStorage(
            [NotNull] this IGlobalConfiguration configuration,
            [NotNull] Func<DbConnection> connectionFactory)
        {
            if (configuration == null) throw new ArgumentNullException(nameof(configuration));
            if (connectionFactory == null) throw new ArgumentNullException(nameof(connectionFactory));

            var storage = new SqlServerStorage(connectionFactory);
            return configuration.UseStorage(storage).UseSqlServerStorageCommonMetrics();
        }

        public static IGlobalConfiguration<SqlServerStorage> UseSqlServerStorage(
            [NotNull] this IGlobalConfiguration configuration,
            [NotNull] Func<DbConnection> connectionFactory,
            [NotNull] SqlServerStorageOptions options)
        {
            if (configuration == null) throw new ArgumentNullException(nameof(configuration));
            if (connectionFactory == null) throw new ArgumentNullException(nameof(connectionFactory));
            if (options == null) throw new ArgumentNullException(nameof(options));

            var storage = new SqlServerStorage(connectionFactory, options);
            return configuration.UseStorage(storage).UseSqlServerStorageCommonMetrics();
        }

        private static IGlobalConfiguration<SqlServerStorage> UseSqlServerStorageCommonMetrics(
            [NotNull] this IGlobalConfiguration<SqlServerStorage> configuration)
        {
            if (configuration == null) throw new ArgumentNullException(nameof(configuration));

            if (Interlocked.Exchange(ref _metricsInitialized, 1) == 0)
            {
                configuration.UseDashboardMetric(SqlServerStorage.SchemaVersion);
                configuration.UseDashboardMetric(SqlServerStorage.ActiveConnections);
                configuration.UseDashboardMetric(SqlServerStorage.TotalConnections);
                configuration.UseDashboardMetric(SqlServerStorage.ActiveTransactions);
                configuration.UseDashboardMetric(SqlServerStorage.DataFilesSize);

View on GitHub (pinned to c236dd0f93)

Solutions

  1. Provide a non-null Func<DbConnection> that creates a fresh, unopened DbConnection.
  2. Validate the factory is non-null before calling.

Example fix

// before
Func<DbConnection> factory = null;
config.UseSqlServerStorage(factory, options);
// after
Func<DbConnection> factory = () => new SqlConnection(connStr);
config.UseSqlServerStorage(factory, options);
Defensive patterns

Strategy: validation

Validate before calling

Func<DbConnection> connectionFactory = () => new SqlConnection(connStr);
if (connectionFactory == null)
    throw new InvalidOperationException("connectionFactory must be provided.");

config.UseSqlServerStorage(connectionFactory, options);

Type guard

static bool IsValidFactory(Func<DbConnection> factory) => factory != null;

Prevention

When it happens

Trigger: Passing null as the Func<DbConnection> to the three-parameter UseSqlServerStorage overload; a factory that was never assigned.

Common situations: DI misconfiguration; uninitialized variable; refactoring that removes the factory creation.

Related errors


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