{"record":{"id":"51aef8f1cf0f90da","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-observablefactoryasync","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'observableFactoryAsync')","messagePattern":"Value cannot be null\\. \\(Parameter 'observableFactoryAsync'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs","lineNumber":740,"sourceCode":"        /// </summary>\n        /// <typeparam name=\"TResult\">The type of the elements in the produced sequence.</typeparam>\n        /// <typeparam name=\"TResource\">The type of the resource used during the generation of the resulting sequence. Needs to implement <see cref=\"IDisposable\"/>.</typeparam>\n        /// <param name=\"resourceFactoryAsync\">Asynchronous factory function to obtain a resource object.</param>\n        /// <param name=\"observableFactoryAsync\">Asynchronous factory function to obtain an observable sequence that depends on the obtained resource.</param>\n        /// <returns>An observable sequence whose lifetime controls the lifetime of the dependent resource object.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"resourceFactoryAsync\"/> or <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 resource factory and observable factory functions will be signaled.</remarks>\n        public static IObservable<TResult> Using<TResult, TResource>(Func<CancellationToken, Task<TResource>> resourceFactoryAsync, Func<TResource, CancellationToken, Task<IObservable<TResult>>> observableFactoryAsync) where TResource : IDisposable\n        {\n            if (resourceFactoryAsync == null)\n            {\n                throw new ArgumentNullException(nameof(resourceFactoryAsync));\n            }\n\n            if (observableFactoryAsync == null)\n            {\n                throw new ArgumentNullException(nameof(observableFactoryAsync));\n            }\n\n            return s_impl.Using(resourceFactoryAsync, observableFactoryAsync);\n        }\n\n        #endregion\n    }\n}\n","sourceCodeStart":722,"sourceCodeEnd":749,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs#L722-L749","documentation":"The async Observable.Using overload requires an observableFactoryAsync delegate that consumes the resource and a CancellationToken to produce the Task<IObservable<TResult>>. A null value is rejected with ArgumentNullException at call time.","triggerScenarios":"Calling Observable.Using(resourceFactoryAsync, observableFactoryAsync) with a null Func<TResource, CancellationToken, Task<IObservable<TResult>>> as the second argument.","commonSituations":"Conditionally built async lambdas that were never assigned; wrapper APIs forwarding nulls; refactoring from the sync overload where the delegate names changed but assignments did not.","solutions":["Pass a valid async lambda, e.g. async (res, ct) => UseResourceAsync(res, ct)","Verify delegate-producing helpers never return null","Use Observable.Empty<TResult>() inside the factory for no-op cases instead of null"],"exampleFix":"// before\nObservable.Using(ct => OpenResourceAsync(ct), null);\n// after\nObservable.Using(ct => OpenResourceAsync(ct), (res, ct) => UseResourceAsync(res, ct));","handlingStrategy":"validation","validationCode":"if (resourceFactoryAsync == null || observableFactoryAsync == null) throw new ArgumentNullException(observableFactoryAsync == null ? nameof(observableFactoryAsync) : nameof(resourceFactoryAsync));\nvar seq = Observable.Using(resourceFactoryAsync, observableFactoryAsync);","typeGuard":"static bool HasAsyncObservableFactory<TResource, TResult>(Func<TResource, CancellationToken, Task<IObservable<TResult>>> f) where TResource : IDisposable => f is not null;","tryCatchPattern":"try\n{\n    var seq = Observable.Using(resourceFactoryAsync, observableFactoryAsync);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"observableFactoryAsync\")\n{\n    throw new InvalidOperationException(\"An async observable factory is required\", ex);\n}","preventionTips":["Build async factory lambdas inline at the Observable.Using call site","Never assign null to Func fields used as factories; initialize with a default no-op","Add argument-null tests to wrappers around the async overload"],"tags":["rx","csharp","null-argument","async"],"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"}