HangfireIO/Hangfire · error · ArgumentNullException

nameOrConnectionString

Error message

nameOrConnectionString

What it means

The SqlServerStorage constructor received a null nameOrConnectionString argument. This is a standard ArgumentNullException guard at the top of the (string, SqlServerStorageOptions) constructor, thrown before any connection-string resolution occurs.

Source

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

            : this(nameOrConnectionString, new SqlServerStorageOptions())
        {
        }

        /// <summary>
        /// Initializes SqlServerStorage from the provided SqlServerStorageOptions and either the provided connection
        /// string or the connection string with provided name pulled from the application config file.       
        /// </summary>
        /// <param name="nameOrConnectionString">Either a SQL Server connection string or the name of 
        /// a SQL Server connection string located in the connectionStrings node in the application config</param>
        /// <param name="options"></param>
        /// <exception cref="ArgumentNullException"><paramref name="nameOrConnectionString"/> argument is null.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="options"/> argument is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="nameOrConnectionString"/> argument is neither 
        /// a valid SQL Server connection string nor the name of a connection string in the application
        /// config file.</exception>
        public SqlServerStorage(string nameOrConnectionString, SqlServerStorageOptions options)
        {
            if (nameOrConnectionString == null) throw new ArgumentNullException(nameof(nameOrConnectionString));
            if (options == null) throw new ArgumentNullException(nameof(options));

            _connectionString = GetConnectionString(nameOrConnectionString);
            _connectionFactory = DefaultConnectionFactory;
            _options = options;

            Initialize();
        }

        /// <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())
        {
        }

View on GitHub (pinned to c236dd0f93)

Solutions

  1. Ensure the nameOrConnectionString variable is assigned a non-null value before constructing the storage.
  2. If the value comes from configuration, provide a fallback or validate it is not null before use.
  3. Use the IGlobalConfiguration extension methods which also guard against this.

Example fix

// before
var connName = config["HangfireConn"]; // null if missing
var storage = new SqlServerStorage(connName, options);
// after
var connName = config["HangfireConn"] ?? throw new InvalidOperationException("HangfireConn not configured");
var storage = new SqlServerStorage(connName, options);
Defensive patterns

Strategy: validation

Validate before calling

string nameOrConnectionString = /* from config or variable */;
if (nameOrConnectionString == null)
    throw new InvalidOperationException("nameOrConnectionString must be set before creating SqlServerStorage.");

var storage = new SqlServerStorage(nameOrConnectionString, options);

Type guard

static bool IsValidConnectionStringArg(string value) => !string.IsNullOrWhiteSpace(value);

Prevention

When it happens

Trigger: Passing a null string to new SqlServerStorage(...) or config.UseSqlServerStorage(null, options); a variable that was never assigned before being passed.

Common situations: Reading a connection string name from configuration that is missing (returns null); passing an uninitialized string variable; refactoring that removes the assignment.

Related errors


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