{"record":{"id":"0adbf808bf6914b9","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-resourcefactoryasync","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'resourceFactoryAsync')","messagePattern":"Value cannot be null\\. \\(Parameter 'resourceFactoryAsync'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs","lineNumber":735,"sourceCode":"        #region + UsingAsync +\n\n        /// <summary>\n        /// Constructs an observable sequence that depends on a resource object, whose lifetime is tied to the resulting observable sequence's lifetime. The resource is obtained and used through asynchronous methods.\n        /// The CancellationToken passed to the asynchronous methods is tied to the returned disposable subscription, allowing best-effort cancellation at any stage of the resource acquisition or usage.\n        /// </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":717,"sourceCodeEnd":749,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Creation.cs#L717-L749","documentation":"The async overload Observable.Using<TResult,TResource>(Func<CancellationToken,Task<TResource>>, ...) needs a resourceFactoryAsync delegate to create the IDisposable resource asynchronously. The API eagerly throws ArgumentNullException when it is null.","triggerScenarios":"Calling the Task-based Observable.Using overload with a null resourceFactoryAsync (Func<CancellationToken, Task<TResource>>) as the first argument.","commonSituations":"Async factory methods assigned conditionally; awaiting configuration that resolves a null delegate; wrappers built over the async overload that pass through nulls from callers.","solutions":["Pass a valid async factory, e.g. async ct => await CreateResourceAsync(ct)","Ensure methods returning Func<CancellationToken,Task<TResource>> are not returning null","Initialize the delegate before invoking Observable.Using"],"exampleFix":"// before\nObservable.Using<int, DbConnection>(null, (conn, ct) => QueryAsync(conn, ct));\n// after\nObservable.Using(ct => OpenConnectionAsync(ct), (conn, ct) => QueryAsync(conn, ct));","handlingStrategy":"validation","validationCode":"if (resourceFactoryAsync == null) throw new ArgumentNullException(nameof(resourceFactoryAsync));\nif (observableFactoryAsync == null) throw new ArgumentNullException(nameof(observableFactoryAsync));\nvar seq = Observable.Using(resourceFactoryAsync, observableFactoryAsync);","typeGuard":"static bool HasAsyncResourceFactory<TResource>(Func<CancellationToken, Task<TResource>> 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 == \"resourceFactoryAsync\")\n{\n    throw new InvalidOperationException(\"An async resource factory is required\", ex);\n}","preventionTips":["Pass async lambdas directly (ct => CreateAsync(ct)) rather than through nullable intermediate variables","Keep sync and async Using call sites clearly separated to avoid delegate-slot mix-ups","Enable nullable reference types to surface unassigned Func fields"],"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-15T23:17:13.987Z"}