{"record":{"id":"c13055360513adeb","repo":"dotnet/reactive","slug":"argument-cannot-be-null-parameter-name-functionasync","errorCode":null,"errorMessage":"Argument cannot be null (Parameter name: functionAsync)","messagePattern":"Argument cannot be null \\(Parameter name: functionAsync\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/FromAsync.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.Concurrency;\nusing System.Threading;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TResult> FromAsync<TResult>(Func<ValueTask<TResult>> functionAsync)\n        {\n            if (functionAsync == null)\n                throw new ArgumentNullException(nameof(functionAsync));\n\n            return Defer(() => StartAsync(functionAsync));\n        }\n\n        public static IAsyncObservable<TResult> FromAsync<TResult>(Func<ValueTask<TResult>> functionAsync, IAsyncScheduler scheduler)\n        {\n            if (functionAsync == null)\n                throw new ArgumentNullException(nameof(functionAsync));\n            if (scheduler == null)\n                throw new ArgumentNullException(nameof(scheduler));\n\n            return Defer(() => StartAsync(functionAsync, scheduler));\n        }\n\n        public static IAsyncObservable<TResult> FromAsync<TResult>(Func<CancellationToken, ValueTask<TResult>> functionAsync)\n        {\n            if (functionAsync == null)\n                throw new ArgumentNullException(nameof(functionAsync));","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/FromAsync.cs#L1-L34","documentation":"The parameterless-scheduler FromAsync overload throws ArgumentNullException when functionAsync is null. FromAsync converts an async function into an IAsyncObservable that emits one result, and it validates the function eagerly so the error surfaces at the factory call rather than at subscribe time.","triggerScenarios":"Calling AsyncObservable.FromAsync((Func<ValueTask<TResult>>)null); storing the ValueTask-returning function in a nullable delegate that was never assigned; conditional handler selection resolving to null.","commonSituations":"Switching from Task-returning methods to ValueTask and leaving the delegate reference null; strategy/factory patterns where no strategy matched; reflection-created delegates that failed to bind.","solutions":["Pass a non-null function, e.g. AsyncObservable.FromAsync(() => new ValueTask<int>(Compute())).","Guard when the function is optional: if (fn != null) var obs = AsyncObservable.FromAsync(fn);","Fix the delegate source (DI registration, factory match) so it never yields null."],"exampleFix":"// before\nFunc<ValueTask<int>> fn = null;\nvar obs = AsyncObservable.FromAsync(fn);\n// after\nFunc<ValueTask<int>> fn = () => new ValueTask<int>(42);\nvar obs = AsyncObservable.FromAsync(fn);","handlingStrategy":"validation","validationCode":"// C#\nif (functionAsync == null) throw new ArgumentNullException(nameof(functionAsync));\nvar obs = AsyncObservable.FromAsync(functionAsync);","typeGuard":"static bool IsValid<TResult>(Func<ValueTask<TResult>> fn) => fn != null;","tryCatchPattern":"try { var obs = AsyncObservable.FromAsync(fn); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"functionAsync\") { /* resolve/assign the function */ }","preventionTips":["Use lambdas directly at the call site rather than nullable delegate variables.","When migrating Task -> ValueTask, update all delegate assignments in the same change.","Fail fast in factories when no handler strategy matches."],"tags":["csharp","argument-null","async-rx","fromasync"],"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"}