{"record":{"id":"7a541156d84617c3","repo":"HangfireIO/Hangfire","slug":"existingconnection","errorCode":null,"errorMessage":"existingConnection","messagePattern":"existingConnection","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Hangfire.SqlServer/SqlServerStorage.cs","lineNumber":117,"sourceCode":"\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"SqlServerStorage\"/> class with\n        /// explicit instance of the <see cref=\"DbConnection\"/> class that will be used\n        /// to query the data.\n        /// </summary>\n        public SqlServerStorage([NotNull] DbConnection existingConnection)\n            : this(existingConnection, new SqlServerStorageOptions())\n        {\n        }\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"SqlServerStorage\"/> class with\n        /// explicit instance of the <see cref=\"DbConnection\"/> class that will be used\n        /// to query the data, with the given options.\n        /// </summary>\n        public SqlServerStorage([NotNull] DbConnection existingConnection, [NotNull] SqlServerStorageOptions options)\n        {\n            if (existingConnection == null) throw new ArgumentNullException(nameof(existingConnection));\n            if (options == null) throw new ArgumentNullException(nameof(options));\n\n            _existingConnection = existingConnection;\n            _options = options;\n\n            Initialize();\n        }\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"SqlServerStorage\"/> class with\n        /// a connection factory <see cref=\"Func{DbConnection}\"/> class that will be invoked\n        /// to create new database connections for querying the data.\n        /// </summary>\n        public SqlServerStorage([NotNull] Func<DbConnection> connectionFactory)\n            : this(connectionFactory, new SqlServerStorageOptions())\n        {\n        }\n","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/HangfireIO/Hangfire/blob/c236dd0f930f831ec151e436e138ddc429a02a72/src/Hangfire.SqlServer/SqlServerStorage.cs#L99-L135","documentation":"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.","triggerScenarios":"Passing null to new SqlServerStorage(existingConnection, options); a DI-injected DbConnection that resolved to null.","commonSituations":"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.","solutions":["Ensure the DbConnection instance is created and non-null before passing it to the constructor.","If using DI, register DbConnection with the correct lifetime (Singleton or Scoped) and connection string.","Validate with a null check before construction if the source may be null."],"exampleFix":"// before\nDbConnection conn = _serviceProvider.GetService<DbConnection>(); // null if not registered\nvar storage = new SqlServerStorage(conn, options);\n// after\nservices.AddSingleton<DbConnection>(sp =>\n    new SqlConnection(Configuration.GetConnectionString(\"Hangfire\")));\n// ...\nvar storage = new SqlServerStorage(conn, options);","handlingStrategy":"validation","validationCode":"DbConnection existingConnection = /* from DI or creation */;\nif (existingConnection == null)\n    throw new InvalidOperationException(\"existingConnection must be a non-null, open DbConnection.\");\n\nvar storage = new SqlServerStorage(existingConnection, options);","typeGuard":"static bool IsUsableConnection(DbConnection conn) => conn != null && conn.State == ConnectionState.Open;","tryCatchPattern":null,"preventionTips":["Register DbConnection in DI with the correct lifetime and connection string.","Ensure the connection is created and optionally opened before passing to the constructor.","Use a null check with a descriptive error if the source may be null."],"tags":["argument-null","constructor","dbconnection","validation"],"backgroundTag":null,"analyzedSha":"c236dd0f930f831ec151e436e138ddc429a02a72","analyzedAt":"2026-08-13T20:27:11.027Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}