{"record":{"id":"539539f326df4685","repo":"dotnet/reactive","slug":"nameof-observablefactoryasync","errorCode":null,"errorMessage":"nameof(observableFactoryAsync)","messagePattern":"nameof\\(observableFactoryAsync\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs","lineNumber":232,"sourceCode":"\n        /// <summary>\n        /// Returns an observable sequence that starts the specified asynchronous factory function whenever a new observer subscribes.\n        /// </summary>\n        /// <typeparam name=\"TResult\">The type of the elements in the sequence returned by the factory function, and in the resulting sequence.</typeparam>\n        /// <param name=\"observableFactoryAsync\">Asynchronous factory function to start for each observer that subscribes to the resulting sequence.</param>\n        /// <param name=\"ignoreExceptionsAfterUnsubscribe\">\n        /// If true, exceptions that occur after cancellation has been initiated by unsubscribing from the observable\n        /// this method returns will be handled and silently ignored. If false, they will go unobserved, meaning they\n        /// will eventually emerge through <see cref=\"TaskScheduler.UnobservedTaskException\"/>.\n        /// </param>\n        /// <returns>An observable sequence whose observers trigger the given asynchronous observable factory function to be started.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"observableFactoryAsync\"/> is null.</exception>\n        /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>\n        public static IObservable<TResult> Defer<TResult>(Func<Task<IObservable<TResult>>> observableFactoryAsync, bool ignoreExceptionsAfterUnsubscribe)\n        {\n            if (observableFactoryAsync == null)\n            {\n                throw new ArgumentNullException(nameof(observableFactoryAsync));\n            }\n\n            return s_impl.Defer(observableFactoryAsync, ignoreExceptionsAfterUnsubscribe);\n        }\n\n        /// <summary>\n        /// Returns an observable sequence that starts the specified cancellable asynchronous factory function whenever a new observer subscribes.\n        /// The CancellationToken passed to the asynchronous factory function is tied to the returned disposable subscription, allowing best-effort cancellation.\n        /// </summary>\n        /// <typeparam name=\"TResult\">The type of the elements in the sequence returned by the factory function, and in the resulting sequence.</typeparam>\n        /// <param name=\"observableFactoryAsync\">Asynchronous factory function to start for each observer that subscribes to the resulting sequence.</param>\n        /// <returns>An observable sequence whose observers trigger the given asynchronous observable factory function to be started.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"observableFactoryAsync\"/> is null.</exception>\n        /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>\n        /// <remarks>When a subscription to the resulting sequence is disposed, the CancellationToken that was fed to the asynchronous observable factory function will be signaled.</remarks>\n        public static IObservable<TResult> DeferAsync<TResult>(Func<CancellationToken, Task<IObservable<TResult>>> observableFactoryAsync)\n        {\n            return DeferAsync(observableFactoryAsync, ignoreExceptionsAfterUnsubscribe: false);","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs#L214-L250","documentation":"This Defer overload uses an asynchronous factory Func<Task<IObservable<TResult>>> plus ignoreExceptionsAfterUnsubscribe. The factory must be non-null; Rx throws ArgumentNullException at composition time rather than letting the first subscription throw.","triggerScenarios":"Calling Observable.Defer<TResult>(observableFactoryAsync, ignoreExceptionsAfterUnsubscribe) with a null Func<Task<IObservable<TResult>>> argument.","commonSituations":"An async factory method group resolved via reflection returning null, an unassigned delegate field, or a settings-driven factory that is null when async creation is disabled.","solutions":["Pass a non-null async factory returning Task<IObservable<TResult>>.","Null-check the factory delegate before calling Defer.","Fix the producer that returns null instead of a delegate."],"exampleFix":"// before\nvar obs = Observable.Defer(asyncFactory, ignoreAfterUnsub); // asyncFactory null\n// after\nvar obs = Observable.Defer(asyncFactory ?? (async () => Observable.Empty<int>()), ignoreAfterUnsub);","handlingStrategy":"validation","validationCode":"if (observableFactoryAsync is null) throw new ArgumentNullException(nameof(observableFactoryAsync));\nvar obs = Observable.Defer(observableFactoryAsync, ignoreExceptionsAfterUnsubscribe);","typeGuard":"bool IsValid<T>(Func<Task<IObservable<T>>> f) => f is not null;","tryCatchPattern":"try { var obs = Observable.Defer(observableFactoryAsync, ignore); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observableFactoryAsync\") { /* use sync Defer fallback */ }","preventionTips":["Ensure async factory method groups are static/instance methods that exist (no reflection nulls)","Validate settings-driven factories before pipeline composition","Mark required delegate parameters non-nullable in your own wrappers"],"tags":["argumentnull","reactive-extensions","async-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"}