dotnet/reactive · error · ArgumentNullException

asyncOperationSelector

Error message

asyncOperationSelector

What it means

The two-argument SelectMany overload throws ArgumentNullException when asyncOperationSelector, the delegate producing an IAsyncOperation<TAsyncOperationResult> per element, is null. Rx validates all delegate arguments before returning the composed sequence. Without this selector no inner asynchronous sequence can be created.

Solutions

  1. Supply a non-null async selector, e.g. x => x.FetchAsync().
  2. Assert or null-check the delegate before building the query.
  3. Fix dependency injection so the selector delegate is registered.

Example fix

// before
query = src.SelectMany(_opSelector, (x, r) => Combine(x, r)); // _opSelector null
// after
_opSelector = x => x.FetchAsync();
query = src.SelectMany(_opSelector, (x, r) => Combine(x, r));
Defensive patterns

Strategy: validation

Validate before calling

if (asyncOperationSelector == null) throw new ArgumentNullException(nameof(asyncOperationSelector));

Type guard

bool HasAsyncOpSelector<TSource, R>(Func<TSource, IAsyncOperation<R>> s) => s != null;

Try / catch

try { return source.SelectMany(opSelector, resultSelector); }
catch (ArgumentNullException ex) when (ex.ParamName == "asyncOperationSelector") { /* provide fallback projection or rethrow */ throw; }

Prevention

When it happens

Trigger: source.SelectMany(asyncOpSelector, resultSelector) with asyncOpSelector null — typically a nullable Func forwarded from a caller or left at its default value.

Common situations: Configuration-driven pipelines where the async selector is injected and missing; optional overloads in helper libraries; renaming refactors that broke the delegate assignment.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/4cf4a2b53a65f525. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Linq/WindowsObservable.StandardSequenceOperators.cs:85

        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <typeparam name="TAsyncOperationResult">The type of the results produced by the projected asynchronous operations.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the result sequence, obtained by using the selector to combine source sequence elements with their corresponding intermediate asynchronous operation results.</typeparam>
        /// <param name="source">An observable sequence of elements to project.</param>
        /// <param name="asyncOperationSelector">A transform function to apply to each element.</param>
        /// <param name="resultSelector">A transform function to apply to each element of the intermediate sequence.</param>
        /// <returns>An observable sequence whose elements are the result of obtaining an asynchronous operation for each element of the input sequence and then mapping the asynchronous operation's result and its corresponding source element to a result element.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="asyncOperationSelector"/> or <paramref name="resultSelector"/> is null.</exception>
        /// <remarks>This overload supports using LINQ query comprehension syntax in C# and Visual Basic to compose observable sequences and Windows Runtime asynchronous operations, without requiring manual conversion of the asynchronous operations to observable sequences using <see cref="AsyncInfoObservableExtensions.ToObservable{TResult}(IAsyncOperation{TResult})"/>.</remarks>
        public static IObservable<TResult> SelectMany<TSource, TAsyncOperationResult, TResult>(this IObservable<TSource> source, Func<TSource, IAsyncOperation<TAsyncOperationResult>> asyncOperationSelector, Func<TSource, TAsyncOperationResult, TResult> resultSelector)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (asyncOperationSelector == null)
            {
                throw new ArgumentNullException(nameof(asyncOperationSelector));
            }

            if (resultSelector == null)
            {
                throw new ArgumentNullException(nameof(resultSelector));
            }

            return source.SelectMany(x => asyncOperationSelector(x).ToObservable(), resultSelector);
        }

        /// <summary>
        /// Projects each element of an observable sequence to a Windows Runtime asynchronous operation, invokes the result selector for the source element and the asynchronous operation result, and merges the results into one observable sequence.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <typeparam name="TAsyncOperationResult">The type of the results produced by the projected asynchronous operations.</typeparam>
        /// <typeparam name="TAsyncOperationProgress">The type of the reported progress objects, which get ignored by this query operator.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the result sequence, obtained by using the selector to combine source sequence elements with their corresponding intermediate asynchronous operation results.</typeparam>
        /// <param name="source">An observable sequence of elements to project.</param>

View on GitHub (pinned to 94b5d5ab91)