{"record":{"id":"53c79bc5c413cf00","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observablefactory","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'observableFactory')","messagePattern":"Value cannot be null\\. \\(Parameter 'observableFactory'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Defer.cs","lineNumber":16,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Reactive.Disposables;\nusing System.Threading;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> Defer<TSource>(Func<IAsyncObservable<TSource>> observableFactory)\n        {\n            if (observableFactory == null)\n                throw new ArgumentNullException(nameof(observableFactory));\n\n            return Defer(() => new ValueTask<IAsyncObservable<TSource>>(observableFactory()));\n        }\n\n        public static IAsyncObservable<TSource> DeferAsync<TSource>(Func<ValueTask<IAsyncObservable<TSource>>> observableFactory) => Defer(observableFactory);\n\n        public static IAsyncObservable<TSource> Defer<TSource>(Func<ValueTask<IAsyncObservable<TSource>>> observableFactory)\n        {\n            if (observableFactory == null)\n                throw new ArgumentNullException(nameof(observableFactory));\n\n            return Create<TSource>(async observer =>\n            {\n                var source = default(IAsyncObservable<TSource>);\n\n                try\n                {\n                    source = await observableFactory().ConfigureAwait(false);","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Defer.cs#L1-L34","documentation":"AsyncObservable.Defer requires a non-null observableFactory Func that produces the IAsyncObservable when subscribed. The library validates it eagerly and throws ArgumentNullException (Parameter 'observableFactory') because deferring a null factory would fail at subscribe time with no useful stack.","triggerScenarios":"Calling AsyncObservable.Defer<TSource>((Func<IAsyncObservable<TSource>>)null); also reachable via DeferAsync or FromAsync, which delegate to Defer, when their factory arguments are null.","commonSituations":"Passing a method group that resolves to null via reflection or conditional delegation; a config-selected factory string that failed to resolve; overloads where Func vs Func<ValueTask<...>> ambiguity led to casting null.","solutions":["Pass a valid factory lambda, e.g. () => AsyncObservable.Return(42).","Check that the variable holding the factory is initialized before calling Defer.","If the factory comes from configuration/DI, validate it resolved successfully before deferring."],"exampleFix":"// before\nFunc<IAsyncObservable<int>> factory = null;\nvar xs = AsyncObservable.Defer(factory);\n// after\nvar xs = AsyncObservable.Defer(() => AsyncObservable.Return(42));","handlingStrategy":"validation","validationCode":"if (observableFactory is null) throw new ArgumentNullException(nameof(observableFactory));\nvar xs = AsyncObservable.Defer(observableFactory);","typeGuard":"static bool IsValidFactory<T>(Func<IAsyncObservable<T>> f) => f is not null;","tryCatchPattern":"try\n{\n    var xs = AsyncObservable.Defer(factory);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observableFactory\")\n{\n    // factory delegate was null; ensure it is assigned before deferring\n}","preventionTips":["Assign factory delegates at construction time, not lazily.","Prefer inline lambdas over nullable delegate fields when the source is static.","If factories come from config/DI, fail fast at startup when resolution returns null."],"tags":["null-argument","rx","defer","factory"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}