{"record":{"id":"f653b1a6455a1213","repo":"HangfireIO/Hangfire","slug":"context-f653b1","errorCode":null,"errorMessage":"context","messagePattern":"context","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Hangfire.Core/Client/BackgroundJobFactory.cs","lineNumber":76,"sourceCode":"            }\n        }\n\n        internal BackgroundJobFactory(\n            [NotNull] IJobFilterProvider filterProvider, \n            [NotNull] IBackgroundJobFactory innerFactory)\n        {\n            if (filterProvider == null) throw new ArgumentNullException(nameof(filterProvider));\n            if (innerFactory == null) throw new ArgumentNullException(nameof(innerFactory));\n\n            _filterProvider = filterProvider;\n            _innerFactory = innerFactory;\n        }\n\n        public IStateMachine StateMachine => _innerFactory.StateMachine;\n\n        public BackgroundJob Create(CreateContext context)\n        {\n            if (context == null) throw new ArgumentNullException(nameof(context));\n\n            var filterInfo = GetFilters(context.Job);\n\n            try\n            {\n                context.Factory = this;\n\n                var createdContext = CreateWithFilters(context, filterInfo.ClientFilters);\n                return createdContext.BackgroundJob;\n            }\n            catch (Exception ex) when (ex.IsCatchableExceptionType())\n            {\n                var exceptionContext = new ClientExceptionContext(context, ex);\n\n                InvokeExceptionFilters(exceptionContext, filterInfo.ClientExceptionFiltersReversed);\n                if (!exceptionContext.ExceptionHandled)\n                {\n                    throw;","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/HangfireIO/Hangfire/blob/c236dd0f930f831ec151e436e138ddc429a02a72/src/Hangfire.Core/Client/BackgroundJobFactory.cs#L58-L94","documentation":"Thrown by BackgroundJobFactory.Create when the CreateContext argument is null. Create is the entry point of the client-side job-creation pipeline: it resolves IClientFilter instances, sets context.Factory = this, and dispatches OnCreating/OnCreated around the inner factory. The null-guard prevents NREs deep in the filter-dispatch and serialization logic. Hangfire's own IBackgroundJobClient.Create path always builds a CreateContext, so a null context implies a custom client or test.","triggerScenarios":"Invoking factory.Create(null) directly; a custom IBackgroundJobClient implementation that calls the factory without building a CreateContext; unit tests stubbing the factory without a context.","commonSituations":"Replacing BackgroundJobClient with a custom wrapper; integration tests that short-circuit the client; DI misconfiguration returning a null context from a factory delegate.","solutions":["Construct a CreateContext with the required storage, connection, job, and initial state before calling Create.","Use the standard IBackgroundJobClient.Create path (e.g., BackgroundJob.Enqueue/ContinueWith/AddOrUpdate) which builds the context for you.","If you own a custom client, guard the context at your boundary and throw a descriptive exception."],"exampleFix":"// before\nvar job = factory.Create(null);\n\n// after\nvar createContext = new CreateContext(storage, connection, jobObj, initialState);\nvar job = factory.Create(createContext);","handlingStrategy":"validation","validationCode":"if (createContext == null) throw new ArgumentException(\"CreateContext is required\", nameof(createContext));\nvar job = factory.Create(createContext);","typeGuard":"public static bool IsValidCreateContext(CreateContext ctx)\n    => ctx != null && ctx.Job != null && ctx.Storage != null;","tryCatchPattern":null,"preventionTips":["Use IBackgroundJobClient.Enqueue/AddOrUpdate/ContinueWith rather than calling factory.Create directly.","In custom clients, build a CreateContext with storage, connection, job, and initial state before invoking the factory.","Guard custom dispatch paths with a null check on the context."],"tags":["argument-null","job-factory","client"],"backgroundTag":null,"analyzedSha":"c236dd0f930f831ec151e436e138ddc429a02a72","analyzedAt":"2026-08-13T20:27:11.027Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}