{"record":{"id":"3741a2592a617abd","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-onerrorasync","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'onErrorAsync')","messagePattern":"Value cannot be null\\. \\(Parameter 'onErrorAsync'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/AsyncObservableExtensions.cs","lineNumber":29,"sourceCode":"    {\n        public static ValueTask<IAsyncDisposable> SubscribeAsync<T>(this IAsyncObservable<T> source, Func<T, ValueTask> onNextAsync)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (onNextAsync == null)\n                throw new ArgumentNullException(nameof(onNextAsync));\n\n            return source.SubscribeAsync(new AsyncObserver<T>(onNextAsync, ex => new ValueTask(Task.FromException(ex)), () => default));\n        }\n\n        public static ValueTask<IAsyncDisposable> SubscribeAsync<T>(this IAsyncObservable<T> source, Func<T, ValueTask> onNextAsync, Func<Exception, ValueTask> onErrorAsync)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (onNextAsync == null)\n                throw new ArgumentNullException(nameof(onNextAsync));\n            if (onErrorAsync == null)\n                throw new ArgumentNullException(nameof(onErrorAsync));\n\n            return source.SubscribeAsync(new AsyncObserver<T>(onNextAsync, onErrorAsync, () => default));\n        }\n\n        public static ValueTask<IAsyncDisposable> SubscribeAsync<T>(this IAsyncObservable<T> source, Func<T, ValueTask> onNextAsync, Func<ValueTask> onCompletedAsync)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (onNextAsync == null)\n                throw new ArgumentNullException(nameof(onNextAsync));\n            if (onCompletedAsync == null)\n                throw new ArgumentNullException(nameof(onCompletedAsync));\n\n            return source.SubscribeAsync(new AsyncObserver<T>(onNextAsync, ex => new ValueTask(Task.FromException(ex)), onCompletedAsync));\n        }\n\n        public static ValueTask<IAsyncDisposable> SubscribeAsync<T>(this IAsyncObservable<T> source, Func<T, ValueTask> onNextAsync, Func<Exception, ValueTask> onErrorAsync, Func<ValueTask> onCompletedAsync)\n        {","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/AsyncObservableExtensions.cs#L11-L47","documentation":"UsingAsync is the ValueTask-based variant of Using for asynchronous resource acquisition. It guards the async resource factory eagerly; a null Func<ValueTask<TResource>> cannot be awaited at subscription time, so ArgumentNullException(nameof(resourceFactory)) is thrown at composition (Using.cs:63).","triggerScenarios":"Calling AsyncObservable.UsingAsync<TResult, TResource>(null, observableFactory) with TResource : IDisposable.","commonSituations":"Async factory supplied only when a connection string/config is present; migration from sync Using to UsingAsync leaving one argument null; factory retrieved from an optional service provider.","solutions":["Pass a non-null Func<ValueTask<TResource>>, e.g. async () => await ConnectAsync()","Wrap sync creation as () => new ValueTask<TResource>(Create())","Null-check async factory delegates resolved at runtime before composing"],"exampleFix":"// before\nFunc<ValueTask<DbConnection>> factory = useAsync ? null : MakeFactory();\nvar op = AsyncObservable.UsingAsync(factory, conn => QueryAsync(conn));\n// after\nvar op = AsyncObservable.UsingAsync(async () => await ConnectAsync(), conn => QueryAsync(conn));","handlingStrategy":"validation","validationCode":"if (resourceFactory is null) throw new ArgumentNullException(nameof(resourceFactory));\nvar op = AsyncObservable.UsingAsync(resourceFactory, observableFactory);","typeGuard":"static bool ValidUsingAsyncArgs<TResource,TResult>(Func<ValueTask<TResource>> rf, Func<TResource,ValueTask<IAsyncObservable<TResult>>> of) => rf != null && of != null;","tryCatchPattern":"try\n{\n    var op = AsyncObservable.UsingAsync(resourceFactory, observableFactory);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"resourceFactory\")\n{\n    // fall back to sync creation: () => new ValueTask<TResource>(CreateSync())\n}","preventionTips":["Wrap sync factories as () => new ValueTask<TResource>(Create()) when migrating to UsingAsync","Assert async factory delegates are assigned during configuration validation","Avoid ternaries that leave a null branch for factory delegates"],"tags":["argument-null","async","resource-management"],"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"}