{"record":{"id":"2c6f81175a93c378","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-taskfactory","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'taskFactory')","messagePattern":"Value cannot be null\\. \\(Parameter 'taskFactory'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Concurrency/TaskPoolScheduler.cs","lineNumber":191,"sourceCode":"            public void Dispose()\n            {\n                _cancel.Dispose();\n            }\n\n            public bool IsDisposed => _cancel.IsDisposed;\n        }\n\n        private static readonly Lazy<TaskPoolScheduler> LazyInstance = new(static () => new TaskPoolScheduler(new TaskFactory(TaskScheduler.Default)));\n        private readonly TaskFactory _taskFactory;\n\n        /// <summary>\n        /// Creates an object that schedules units of work using the provided <see cref=\"TaskFactory\"/>.\n        /// </summary>\n        /// <param name=\"taskFactory\">Task factory used to create tasks to run units of work.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"taskFactory\"/> is <c>null</c>.</exception>\n        public TaskPoolScheduler(TaskFactory taskFactory)\n        {\n            _taskFactory = taskFactory ?? throw new ArgumentNullException(nameof(taskFactory));\n        }\n\n        /// <summary>\n        /// Gets an instance of this scheduler that uses the default <see cref=\"TaskScheduler\"/>.\n        /// </summary>\n        public static TaskPoolScheduler Default => LazyInstance.Value;\n\n        /// <summary>\n        /// Schedules an action to be executed.\n        /// </summary>\n        /// <typeparam name=\"TState\">The type of the state passed to the scheduled action.</typeparam>\n        /// <param name=\"state\">State passed to the action to be executed.</param>\n        /// <param name=\"action\">Action to be executed.</param>\n        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"action\"/> is <c>null</c>.</exception>\n        public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)\n        {\n            if (action == null)","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Concurrency/TaskPoolScheduler.cs#L173-L209","documentation":"The TaskPoolScheduler constructor throws ArgumentNullException when the TaskFactory is null. The scheduler relies on the factory to start tasks for each scheduled work item; a null factory would break every Schedule call, so Rx validates eagerly. TaskPoolScheduler.Default is the ready-made instance using TaskScheduler.Default.","triggerScenarios":"new TaskPoolScheduler((TaskFactory)null), usually a TaskFactory field/property that was never initialized or a DI registration that yielded null.","commonSituations":"Configuring a custom TaskFactory (e.g. with a specific TaskScheduler or CancellationToken) where the configuration step silently failed; refactoring that removed factory initialization.","solutions":["Pass a valid TaskFactory, e.g. new TaskFactory() or new TaskFactory(cancellationToken, TaskCreationOptions, TaskContinuationOptions, taskScheduler).","Use TaskPoolScheduler.Default if no custom factory is required.","Fix the DI/config wiring that produced a null TaskFactory."],"exampleFix":"// before\nvar sched = new TaskPoolScheduler(_taskFactory); // null\n// after\nvar sched = _taskFactory != null\n    ? new TaskPoolScheduler(_taskFactory)\n    : TaskPoolScheduler.Default;","handlingStrategy":"validation","validationCode":"if (taskFactory == null) taskFactory = TaskPoolScheduler.Default; // or new TaskFactory()","typeGuard":"bool hasFactory = taskFactory is not null;","tryCatchPattern":"try { var sched = new TaskPoolScheduler(taskFactory); }\ncatch (ArgumentNullException) { var sched = TaskPoolScheduler.Default; }","preventionTips":["Use TaskPoolScheduler.Default unless a custom factory is truly needed","Initialize TaskFactory fields at declaration or via constructor injection","Verify DI registrations supply a TaskFactory instance"],"tags":["rx","scheduler","taskfactory","null-argument"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}